How to handle custom firebase token expiry in Firebase 3.x.x

后端 未结 4 1817
孤街浪徒
孤街浪徒 2021-02-04 04:24

I have used \"signInWithCustomToken()\" to authenticate firebase user.

This token expires in 1 hour.

Firebase has recommended tok

4条回答
  •  感动是毒
    2021-02-04 04:34

    Something like this is a way to check the Token and see if its expired. You can then mint a new one

    public async Task GetIdTokenAsync()
    {
      // Get current time in seconds from Epoch
      var secondsSinceEpoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
    
      // We have already minted a token for this session, so check if we need a new one 
      if (this.idToken != null)
      {
        // Check to see if current token is expired or will expire soon
        var idTokenExpiration = JwtDecoder.TokenExpirationTime(this.idToken);
        if (idTokenExpiration > (secondsSinceEpoch - 60L))
        {
          return this.idToken;
        }
      }
    
      // No id token for this session, or we have an id token, but its expired, so mint a new one
      this.idToken = await auth?.CurrentUser?.GetIdTokenAsync(true);
      return this.idToken;
    }
    

提交回复
热议问题