Skip to content

Refresh access token #219

Open
Open
@ngocduong777

Description

@ngocduong777

Hello everyone, I have just started learning about Firebase Authentication recently, and as I understand it, the token will expire after 3600 seconds (which is 1 hour) by default and needs to be refreshed.
I am building a WPF application, and my code looks like the one below.
I am not sure if the code I am writing is correct,
but I want the case where the user logs in -> leaves the app open for more than 1 hour, and the token will automatically be refreshed to continue being used.
I appreciate all your suggestions.

public AuthenticationStore(FirebaseAuthClient firebaseAuthClient)
{
     _firebaseAuthClient = firebaseAuthClient;
     _firebaseAuthClient.AuthStateChanged += OnAuthStateChanged;
}

private async void OnAuthStateChanged(object? sender, UserEventArgs e)
{
    await Application.Current.Dispatcher.Invoke(async () =>
    {
        if (e.User != null)
        {
            if (e.User.Credential.IsExpired())
            {
                await GetFreshAuthAsync();
            }
        }

    });
}
 public async Task<string> GetFreshAuthAsync()
 {
     if (_firebaseAuthClient.User == null)
     {
         return null;
     }

     string newToken = await _firebaseAuthClient.User.GetIdTokenAsync();
     return newToken;
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions