在AngularFireAuth中抓取当前用户 [英] Grabbing the current user in AngularFireAuth

查看:53
本文介绍了在AngularFireAuth中抓取当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何抓住已登录的用户?

How do I grab the logged in user?

我正在这样做: console.log('正在登录...'); this.afAuth.auth.signInWithEmailAndPassword(this.email,this.password);

I am doing this: console.log('Logging in...'); this.afAuth.auth.signInWithEmailAndPassword(this.email, this.password);

但是文档尚不清楚如何使用户使用代码.它们在模板中显示了一种方式,但是我需要存储当前用户.

But the docs are not clear on how to get the user in code. They show a way in the template but I need to store the current user.

推荐答案

要获取当前用户:

let user = AngularFireAuth.auth.currentUser;
// returns user object

由于某种原因,每次页面刷新都会导致currentUser消失,并且我将用户ID(UID)存储在本地存储中,因此我能够记住该用户并稍后引用Firestore:

For some reason each page refresh causes currentUser to disappear and I store user id (UID) in local storage, so I am able to remember the user and reference to Firestore later:

localStorage.setItem('userUID', user.uid);
// ...
let userID = localStorage.getItem('userUID');
AngularFirestore.firestore.collection('users')
      .doc(userID)
      // ...

注销后,您只需注销用户并删除本地存储值:

On logout you simply logout user and remove local storage value:

logout() {
   AngularFireAuth.auth.signOut().then(() => {
      localStorage.removeItem('userUID');
   };
}

这篇关于在AngularFireAuth中抓取当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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