最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 ponentDidMount (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.

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 ponentDidMount (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.

Share Improve this question asked Dec 21, 2018 at 6:50 JbbaeJbbae 1,03810 silver badges20 bronze badges 1
  • Is this happening on mobile devices, the browser, or both? – Clay Banks Commented Mar 15, 2019 at 23:25
Add a ment  | 

2 Answers 2

Reset to default 3

firebase.auth().getRedirectResult() - is called after the page loads. Official doc link: https://firebase.google./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();
            }
        });
    }

You should call firebase.auth().getRedirectResult() only when user has been authenticated. Example

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    const result = await firebase.auth().getRedirectResult();
    assert(result.user, 'user is empty')
  }
});
发布评论

评论列表(0)

  1. 暂无评论