javascriptreactjsgoogle-authentication

Google Login in React is not triggering events


Im using React to build an app and i'm using react-google-login. The login works, but the onSuccess event is not triggered The HTTP request is working and returns all the information

export class Auth extends Component {

    constructor(props) {
        super(props);
    
        this.state = {
          isLogined: false,
          accessToken: ''
        };
    
        this.login = this.login.bind(this);
        this.handleLoginFailure = this.handleLoginFailure.bind(this);
        this.logout = this.logout.bind(this);
        this.handleLogoutFailure = this.handleLogoutFailure.bind(this);
      }
    
      login (response) {
          console.log('login!!');
        if(response.accessToken){
          this.setState(state => ({
            isLogined: true,
            accessToken: response.accessToken
          }));
        }
      }
    
      logout (response) {
        this.setState(state => ({
          isLogined: false,
          accessToken: ''
        }));
      }
    
      handleLoginFailure (response) {
        alert('Failed to log in')
      }
    
      handleLogoutFailure (response) {
        alert('Failed to log out')
      }
    
    render() {
        return (
            <div>
               
                <GoogleLogin
                    clientId="CLIENTID"
                    buttonText='Login'
                    onSuccess={ this.login }
                    onFailure={ this.handleLoginFailure }
                    cookiePolicy={ 'single_host_origin' }
                    responseType='code,token'
                />
                
            </div>
            )
        }

}
export default Auth;

I've tried some tutorials and watch the documentation but nothing works


Solution

  • Give this a try:

    export class Auth extends Component {
        state = {
          isLogined: false,
          accessToken: ''
        };
        
        login = (response) => {
            console.log('login!!');
          if(response.accessToken){
            this.setState(state => ({
              isLogined: true,
              accessToken: response.accessToken
            }));
          }
        }
        
        render() {
          return (
            <div>
    
              <GoogleLogin
                  clientId="CLIENTID"
                  buttonText='Login'
                  onSuccess={login}
                  cookiePolicy={ 'single_host_origin' }
                  responseType='code,token'
              />
    
            </div>
          )
        }
    }
    export default Auth;