本文介绍了firebase.auth().currentUser为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Angular 4应用中检索当前登录的Firebase用户的UID.但是,如果我将'firebase.auth().currentUser'登录到控制台,则会得到'null'.因此,'firebase.auth().currentUser.uid'给出了'无法读取属性'uid'为null'的错误.

I'm trying to retrieve the UID of the current logged-in firebase user in my Angular 4 app. Though, if I'm logging 'firebase.auth().currentUser' to the console I get a 'null'. So 'firebase.auth().currentUser.uid' gives an 'Cannot read property 'uid' of null' error.

根据本文 https://firebase.google.com/docs/auth/web/manage-users ,这表示我当前尚未登录.尽管如此,我可以从数据库中读取数据(我的规则不允许未经身份验证的用户)和登录方法没有给出任何错误.

According to this article https://firebase.google.com/docs/auth/web/manage-users that would mean that I'm currently not logged in. Though, I am, since I can read data from my database (my rules do not allow unauthenticated users), and the login method did not gave any errors.

我还可以看到具有uid 1的用户已在firebase控制台中登录(.../u/1/project/ my-project /authentication/users)

I can also see that the user with uid 1 is logged in in the firebase console (.../u/1/project/my-project/authentication/users)

我的数据库规则:

{
  "rules": {
    "users": {
      "$uid": {
        ".write": "$uid === auth.uid",
        ".read": "$uid === auth.uid"
      }
    }
   }
}

注意:我没有使用任何Firebase登录方法.我正在使用我的php后端来验证用户并为firebase生成自定义令牌,完全如此处所述: https://firebase.google.com/docs/auth/admin/create-custom-tokens .

Angular 4登录代码:

constructor(private afAuth: AngularFireAuth) {
    this.login();
}

login() {
    console.log('We are logging in on Firebase :)');
    firebase.auth().signInWithCustomToken(localStorage.getItem('firebase_token')).catch(function (error) {
        console.log(error.message);
        console.log('You are not logged in!');
    });
    this.user = firebase.auth().currentUser;
    console.log(firebase.auth().currentUser);
    console.log(firebase.auth().currentUser.uid);
}

控制台输出:

chat.service.ts:59 We are logging in on Firebase :)
chat.service.ts:65 null
ChatComponent_Host.html:1 ERROR TypeError: Cannot read property 'uid' of null
    at ChatService.Array.concat.ChatService.login (chat.service.ts:66)
    at new ChatService (chat.service.ts:23)
    at _createClass (core.es5.js:9572)
    at _createProviderInstance$1 (core.es5.js:9544)
    at resolveNgModuleDep (core.es5.js:9529)
    at NgModuleRef_.get (core.es5.js:10615)
    at resolveDep (core.es5.js:11118)
    at createClass (core.es5.js:10971)
    at createDirectiveInstance (core.es5.js:10802)
    at createViewNodes (core.es5.js:12230)
    at createRootView (core.es5.js:12125)
    at callWithDebugContext (core.es5.js:13506)
    at Object.debugCreateRootView [as createRootView] (core.es5.js:12823)
    at ComponentFactory_.create (core.es5.js:9916)
    at ComponentFactoryBoundToModule.create (core.es5.js:3333)

推荐答案

我认为您应该将代码修改为以下内容,然后重试:-

I think you should modify your code to the following and try again :-

constructor(private afAuth: AngularFireAuth) {
    this.login();
}

login() {
    console.log('We are logging in on Firebase :)');
    firebase.auth().signInWithCustomToken(localStorage.getItem('firebase_token'))
    .then(success=>{
    this.user = firebase.auth().currentUser;
    console.log(firebase.auth().currentUser);
    console.log(firebase.auth().currentUser.uid);
    })
   .catch(function (error) {
        console.log(error.message);
        console.log('You are not logged in!');
    });

}

通过使用then,您可以确保在访问登录的用户对象之前成功返回了登录承诺.

By using then, you'd make sure that the sign in promise was returned successfully before you access the signed in user object.

这篇关于firebase.auth().currentUser为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 21:24