Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh access token #219

Open
ngocduong777 opened this issue Jun 27, 2024 · 0 comments
Open

Refresh access token #219

ngocduong777 opened this issue Jun 27, 2024 · 0 comments

Comments

@ngocduong777
Copy link

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;
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant