为什么使用Firebase的"getRedirectResults()"返回"{user:null}"? [英] Why is Firebase's "getRedirectResults()" returning "{user: null}"?

查看:106
本文介绍了为什么使用Firebase的"getRedirectResults()"返回"{user:null}"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当用户通过社交身份验证(通过重定向)时,它会在 Firebase身份验证下成功创建用户,但无法检索Google/Facebook API的数据.对于认证成功后如何无法检索数据感到困惑.

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.

我以前只拥有 SignInWithPopup (效果很好),但是我试图让 getRedirectResults 也能工作(移动版).

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

鉴于我所看到的行为,该问题很可能与 getRedirectResults 有关.很有可能不是社交API设置,因为否则用户将永远无法在身份验证中创建.

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.

以下代码位于 componentDidMount (这是一个React.js应用程序)中:

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.

推荐答案

firebase.auth().getRedirectResult()-在页面加载后调用. 官方文档链接: https://firebase.google.com/docs/auth/网络/谷歌登录

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

如果没有用户登录,则user将为null.

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

实时示例:

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

            if(user !== null){
                this.setState({ userIsLoggedIn: true });
                this.props.toggleLogIn();
            }
        });
    }

这篇关于为什么使用Firebase的"getRedirectResults()"返回"{user:null}"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆