javascript - AngularFire getIdToken(true)不刷新 token

标签 javascript angular firebase-authentication angularfire

我有一个 angularfire 身份验证服务,它工作得非常好——除了它不会刷新 token 。
我的刷新代码:

firebase.auth().currentUser.getIdToken(true).then((token: string) => {
    const newUser = { token: token }; // new user details in brief
    this.user.next(newUser);
});
调用firebase服务很好并给出响应,但它只返回当前 token 并且不刷新 token - 所以它仍然会在1小时后注销应用程序。
我已经阅读了许多关于 getIdToken 的问题,但没有一个专门解决 token 不刷新的问题。
我做了一个stackblitz:https://stackblitz.com/edit/angular-ivy-uvcdz7?file=src/app/app.component.ts所以你可以看到完整的代码(虽然由于某种原因它不起作用,但也因为我无法弄清楚如何通过 SB 将项目连接到 firebase)
编辑
为了帮助还有其他一些我没想到的奇怪行为,例如在我的 ngOnInit 中,我有......
this.fAuth.onAuthStateChanged((state) => {
    console.log(state);
})
当我登录或注销时它什么都不做 - 没有消息说状态已经改变。当身份验证中的事情发生变化时,我希望触发的这些功能似乎都没有做任何事情。
编辑 2
作为补充信息,在我添加赏金之前,我对 Angular 和 AngularFire 进行了全面而痛苦的更新,所以现在 Angular 是 v13,AngularFire 是 7.2.0,看看这是否会有所不同,但它没有,getIdToken 仍然提供没有刷新 token 。
编辑 3
因此,此功能作为以下答案提供:
public getToken$(): Observable<firebase.auth.IdTokenResult> {
    return from(this.fAuth.currentUser).pipe(switchMap(user => {
        return from(user.getIdTokenResult(true)).pipe(map(token => {
            console.log(token);
            return token;
        }));
    }));
}
它返回一个用户对象:
{
“ claim ”:{
"name": "Bogomip",
"图片": "piccie",
“管理员”:是的,
"iss": "https://securetoken.google.com/repo",
“aud”:“ repo ”,
“auth_time”:1638464159,
"user_id": "用户 ID",
“子”:“用户ID”,
“iat”:1638464323,
“exp”:1638467923,
“电子邮件”:“email@gmail.com”,
“email_verified”:是的,
“火力基地”:{
“身份”:{
“电子邮件”: [
“电子邮件@gmail.com”
]
},
“sign_in_provider”:“密码”
}
},
“ token ”:“这是 token ”,
"authTime": "2021 年 12 月 2 日星期四 16:55:59 GMT",
"issuedAtTime": "2021 年 12 月 2 日星期四 16:58:43 GMT",
"expirationTime": "2021 年 12 月 2 日星期四 17:58:43 GMT",
"signInProvider": "密码",
“signInSecondFactor”:空
}
这是在该函数中记录的“ token ”,但是这个(token.token)中的 token 是刷新ID还是实际 token ?因为它没有改变,但如果它是刷新 token ,那将是有意义的......我们有没有看到实际的 token ?

最佳答案

Firebase 处理 token 刷新过程,因此无需手动刷新 token 。 Firebase 身份验证状态可以从“authState”属性中读取。


@Injectable({
  providedIn: 'root',
})
export class AuthenticationService implements OnInit {

  public readonly userId$: Observable<string>;

  public readonly profile$: Observable<MyProfile>;


  constructor(
    public firestore: AngularFirestore,
    public fAuth: AngularFireAuth,
    private router: Router,
  ) {

    this.userId$ = this.fAuth.authState.pipe(
      map(fbUser => fbUser ? fbUser.uid : null),
      distinctUntilChanged(),
      tap(uid => {
        console.log('AuthService: userId$', uid);
      }),
      shareReplay(1),
    );


    this.profile$ = this.authService.userId$.pipe(
      switchMap(authId => {
        if (authId) {
          return this.getById(authId);
        } else {
          return EMPTY;
        }
      }),
      tap(profile => {
        console.log('[AuthService] profile$', profile);
      }),
      shareReplay(1),
    );
  }

  public getById(id: string): Observable<MyProfile> {
    return this.firestore.doc<MyProfile>('users/'+id).get();
  }

}

关于javascript - AngularFire getIdToken(true)不刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70066502/

相关文章:

swift - 密码重置 swift 4 firebase

javascript - 在 for 循环 js 中创建变量名称不起作用

javascript - 如何在 vis.js 中动态过滤链接

javascript - 传单和 chroma.js .domain 参数不起作用

angular - ngForIn 可以用在 Angular 4 中吗?

angularjs - 手动粘贴 URL 时无法加载文件 Angular 2 路由?

firebase - 在哪里可以找到我的 Firebase apiKey 和 authDomain

javascript - NVDA 不读取带有 aria-label 的 aria-desiredby

javascript - 如何在 Github Pages 上使用 Angular 2 建立网站

android - SMS Retriever API 不适用于 Firebase PhoneAuth