Angular 我是否每次都需要从Firebase获取用户令牌

Angular 我是否每次都需要从Firebase获取用户令牌,angular,typescript,firebase,firebase-authentication,ionic4,Angular,Typescript,Firebase,Firebase Authentication,Ionic4,我将下面的方法放在Auth服务上,获取currentUserIdToken并将其存储为全局变量。这种方法有什么问题吗?i、 它会很快使这个代币过期吗?还是我每次都要从消防基地得到这个?我已经采用了这种方法,因为我可以避免对db进行不必要的调用。有什么想法吗 init(): void { this.afAuth.authState.subscribe(async (user: firebase.User) => { if (user) { this.cur

我将下面的方法放在
Auth
服务上,获取
currentUserIdToken
并将其存储为全局变量。这种方法有什么问题吗?i、 它会很快使这个代币过期吗?还是我每次都要从消防基地得到这个?我已经采用了这种方法,因为我可以避免对db进行不必要的调用。有什么想法吗

init(): void {
    this.afAuth.authState.subscribe(async (user: firebase.User) => {
      if (user) {
        this.currentUserUid = user.uid;
      this.currentUserIdToken = await this.afAuth.auth.currentUser.getIdToken(true); // Here

        this.onboardingService.currentUserUid = this.currentUserUid;


    });
  }

令牌将每小时过期一次。与反复调用
getIdToken()
不同,您可以使用监听对它的更改,并提供回调,以便在客户端SDK自动刷新新令牌时立即接收新令牌

另见:


我可以有一个
AngularFireAuth
方法吗?似乎没有这样的事情不?你有没有试着搜索文档?它只是核心Firebase库的包装。如果找不到它,请在angularfire GitHub上提交一个问题。是的,它有
this.afAuth.idToken