how to refresh token with AngularFireAuth firebase

Viewed 123

I am using "@angular/fire/auth", AngularFireAuth library. I don't see the refresh token method. Is there a way to fresh token using firebase? getIdToken says it will refresh the token if expired but it does not

thanks

if ( AuthUtils.isTokenExpired(this.accessToken) )
{
    return of(false);
}


static isTokenExpired(token: string, offsetSeconds?: number): boolean
{
    // Return if there is no token
    if ( !token || token === '' )
    {
        return true;
    }

    const date = this._getTokenExpirationDate(token);

    offsetSeconds = offsetSeconds || 0;

    if ( date === null )
    {
        return true;
    }

    return !(date.valueOf() > new Date().valueOf() + offsetSeconds * 1000);
}
1 Answers

Firebase by default have an expiration time of an hour. You can get it by getting a result of a token like bellow.

You don't need to refresh tokens by yourself. Firebase do itself automatically. Only when user need to make some crucial changes with his account, he needs to log in with password. For example, he needs to pass a password when he wants to delete an account.

Typescript example

async function getTokenRes() {
   const user = auth.currentUser
   const token = await user.getIdTokenResult()
   console.log(token.expirationTime)
}

And here you have an answer to how to set up your own tokens with expiration time you want: Firebase auth set custom expiration time for custom token

Edit

Here you have example how you can force to refresh token:

async function refreshToken() {
   const user = auth.currentUser
   const token = await user.getIdToken(true)
}
Related