如何抓取已登录的用户?我在做: console.log('Logging in ...'); this.afAuth.auth.signInWithEmailAndPassword(this.email,this.password);在AngularFireAuth中抓取当前用户
但文档不清楚如何让代码中的用户。他们在模板中显示一种方式,但我需要存储当前用户。
如何抓取已登录的用户?我在做: console.log('Logging in ...'); this.afAuth.auth.signInWithEmailAndPassword(this.email,this.password);在AngularFireAuth中抓取当前用户
但文档不清楚如何让代码中的用户。他们在模板中显示一种方式,但我需要存储当前用户。
要获取当前用户:
let user = AngularFireAuth.auth.currentUser;
// returns user object
出于某种原因,每个页面刷新导致currentUser消失,我存储在本地存储的用户ID(UID),所以我能够记住用户和参考后来公司的FireStore:
localStorage.setItem('userUID', user.uid);
// ...
let userID = localStorage.getItem('userUID');
AngularFirestore.firestore.collection('users')
.doc(userID)
// ...
在注销您只需注销用户,并删除本地存储值:
logout() {
AngularFireAuth.auth.signOut().then(() => {
localStorage.removeItem('userUID');
};
}
我想通了!
console.log('Logging in...');
this.afAuth.auth.signInWithEmailAndPassword(this.email, this.password).then((user) => {
this.user = this.afAuth.authState;
});