Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Firebase's "getRedirectResults()" returning "{user: null}"?

Currently, when the user goes through the Social auth (via redirects), it successfully creates a user under Firebase Authentication, but fails to retrieve the Google/Facebook API's data. Perplexed as to how it's failing to retrieve the data when the Auth is successful.

I used to only have the SignInWithPopup (which works perfectly fine), but am trying to get getRedirectResults to work too (for mobile).

Given the behavior I've been seeing, the problem is most likely with the getRedirectResults. Most likely not the social API setup, because otherwise the user would never get created in Authentication.

The following code resides in componentDidMount (this is a React.js app):

if (this.state.device === 'mobile') {
  firebase.auth().getRedirectResult().then(function(result) {
    console.log('login successful! Data is:');
    console.log(result);

    if (result.credential) {
      var provider = result.credential.providerId;
      self.handleSocialAuth(provider, result);
    }
  }).catch(function(error) {
    var errorCode = error.code;
    var errorMessage = error.message;
  });
}

results is supposed to contain the user's data, keeps returning:

login successful! Data is:
{user: null}

I have not deviated from official docs examples, which is why I'm confused.

like image 948
Jbbae Avatar asked Dec 21 '18 06:12

Jbbae


1 Answers

firebase.auth().getRedirectResult() - is called after the page loads. Official doc link: https://firebase.google.com/docs/auth/web/google-signin

Use the below method to retrieve the user that is logged in.

firebase.auth().onAuthStateChanged(function(user) {
          console.log(user);
});

If no user is logged in the user will be null.

Live example:

checkAuthStatus(){
        firebase.auth().onAuthStateChanged(user => {
            this.setState({ btnWithImg: user });

            if(user !== null){
                this.setState({ userIsLoggedIn: true });
                this.props.toggleLogIn();
            }
        });
    }
like image 160
Sergiu Mare Avatar answered Oct 17 '22 04:10

Sergiu Mare