1

I have put the below method on the Auth service and get the currentUserIdToken and store it as a global variable. Is this approach has any issues? i.e. will it expire this token quickly or something like that? Or do I need to get this each and every time from the Firebase? I have done this kind of approach since I can avoid unnecessary calls to the db. Any thoughts?

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;


    });
  }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Sampath
  • 63,341
  • 64
  • 307
  • 441

1 Answers1

2

The token will expire every hour. Instead of making a call to getIdToken() repeatedly, you can listen for changes to it using onIdTokenChanged() and provide a callback to receive the new token immediately when it's automatically refreshed by the client SDK.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441