【问题标题】:Do I need to get the User Token each and every time from the Firebase [closed]我是否需要每次都从 Firebase 获取用户令牌 [关闭]
【发布时间】:2020-05-04 12:32:15
【问题描述】:

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

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

    另见:

    【讨论】:

    • 我可以有一个AngularFireAuth 方法吗?好像没有这种事吧?
    • 您是否尝试搜索文档?它只是核心 Firebase 库的包装器。如果找不到,请在 angularfire GitHub 上提交问题。
    • 是的,它有this.afAuth.idToken
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多