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

User becomes null after a while #228

Open
poochie89 opened this issue Jan 6, 2025 · 0 comments
Open

User becomes null after a while #228

poochie89 opened this issue Jan 6, 2025 · 0 comments

Comments

@poochie89
Copy link

I have a maui app and in the class below to perform the Token refresh, but after a while, the user will be null
How can i prevent the app from loosing the User object? Is there something super basic I am missing?
This is on Windows

builder.Services.AddSingleton(new FirebaseAuthClient(new FirebaseAuthConfig()
{

    ApiKey = "*****",
    AuthDomain = AppConfig.FirebaseAuth,
    Providers = new FirebaseAuthProvider[]
    {
        new EmailProvider()
    },
    
    UserRepository = new FileUserRepository("EvolveFootballUser")
}));



    public class FirebaseDatabaseClient
    {
        public FirebaseAuthClient AuthClient { get; set; }
        public FirebaseClient DatabaseClient { get; private set; }
        public UserService UserService { get; set; }


        public FirebaseDatabaseClient(FirebaseAuthClient authClient, UserService userService)
        {
            AuthClient = authClient;
            UserService = userService;

            DatabaseClient = new FirebaseClient(
               AppConfig.FireBaseDatabase,
               new FirebaseOptions
               {
                   AuthTokenAsyncFactory = () => LoginAsync()
               });
        }


        /// <summary>
        /// Retrieves (and refreshes if necessary) the current user's Firebase ID token.
        /// </summary>
        private async Task<string> LoginAsync()
        {
            if (AuthClient.User == null)
                throw new UnauthorizedAccessException("User is not authenticated.");

            // This forces a token refresh if the current one is expired or near expiry.
            var token = await AuthClient.User.GetIdTokenAsync();

            return token;
        }

    }
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