[英]firebase.auth().currentUser is null
I'm trying to retrieve the UID of the current logged-in firebase user in my Angular 4 app. 我正在尝试在Angular 4应用程序中检索当前登录的firebase用户的UID。 Though, if I'm logging 'firebase.auth().currentUser' to the console I get a 'null'. 但是,如果我将'firebase.auth()。currentUser'记录到控制台,我会得到一个'null'。 So 'firebase.auth().currentUser.uid' gives an 'Cannot read property 'uid' of null' error. 因此'firebase.auth()。currentUser.uid'给出'无法读取属性'uid'of null'错误。
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. 根据这篇文章https://firebase.google.com/docs/auth/web/manage-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) 我还可以看到具有uid 1的用户登录了firebase控制台(... / u / 1 / project / my-project / authentication / users)
My database rules: 我的数据库规则:
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
}
}
}
Note: I'm not using any of the Firebase login methods. 注意:我没有使用任何Firebase登录方法。 I'm using my php back-end to verify users and generate a custom token for firebase, exactly as described here: https://firebase.google.com/docs/auth/admin/create-custom-tokens . 我正在使用我的php后端来验证用户并为firebase生成自定义令牌,完全如下所述: https : //firebase.google.com/docs/auth/admin/create-custom-tokens 。
Angular 4 login code: 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);
}
Console output: 控制台输出:
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)
You are using the currentUser variable when firebase still has not loaded data from local. 当firebase仍然没有从本地加载数据时,您正在使用currentUser变量。 So, in order to know when the currentUser variable is initiated you can check the callback for onAuthStateChanged. 因此,为了知道启动currentUser变量的时间,您可以检查onAuthStateChanged的回调。
var currentUser;
firebase.auth().onAuthStateChanged(function(user){
// user hols the reference to currentUser variable.
});
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!');
});
}
By using then
, you'd make sure that the sign in promise was returned successfully before you access the signed in user object. 通过使用then
,您可以确保在访问登录用户对象之前已成功返回登录承诺。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.