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

问题描述 投票:0回答:1

我将以下方法放在Auth服务上,并获取currentUserIdToken并将其存储为全局变量。这种方法有什么问题吗?即它会很快使此令牌过期或类似的过期?还是我每次都需要从Firebase获取此信息?我已经做了这种方法,因为我可以避免不必要的对数据库的调用。有什么想法吗?

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;


    });
  }
angular typescript firebase firebase-authentication ionic4
1个回答
0
投票

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

另请参见:

© www.soinside.com 2019 - 2024. All rights reserved.