首页 > 解决方案 > 我是否需要每次都从 Firebase 获取用户令牌

问题描述

我已将以下方法放在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;


    });
  }

标签: angulartypescriptfirebasefirebase-authenticationionic4

解决方案


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

也可以看看:


推荐阅读