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

ぃ、小莉子 提交于 2020-05-29 03:16:29

问题


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.


回答1:


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();
            }
        });
    }


来源:https://stackoverflow.com/questions/53880369/why-is-firebases-getredirectresults-returning-user-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!