Skip to content

Keep getting 401 after about 1 hour of subscription #331

@AndreasReitberger

Description

@AndreasReitberger

I have a subscription to my Firebase.Database, however, if the application runs for 60 minutes, it throws an exception.
I found some old issues and a PR for the token refreshing, however it still is not working for me.

Here is what I have.

// Create a client with the `login` function.
public FirebaseClient ConnectWithLogin(Func<Task<string>> login)
{
    return new(baseUri, new FirebaseOptions
    {
        AuthTokenAsyncFactory = async() => await login().ConfigureAwait(false)
    });
}

The login function looks like this.

public async Task<string> LoginSubscribeAndGetTokenAsync(string username, string password)
{
    if (authClient is null || CurrentUser is null) return string.Empty;
    // Avoid multiple logins if the session is actually still valid and the user hasn't changed
    if(CurrentUser?.User?.Info?.Email == username && CurrentUser?.User?.Credential.IsExpired() is false)
    {
        return CurrentUser?.User?.Credential?.IdToken ?? string.Empty;
    }
    CurrentUser = await authClient.SignInWithEmailAndPasswordAsync(username, password);
    // Refresh the token, so that the client is updated afterwards
    Token = await CurrentUser.User.GetIdTokenAsync() ?? string.Empty;
    return Token;
}
// The function for starting the subscription
public void Subscribe<T>(Action<Tuple<string, T>> action, string username, string password, string child = "settings")
{
    try
    {
        // Don't sync user data for anonymous logins
        if (CurrentUser?.User is null || CurrentUser.User.IsAnonymous == true) return;
        if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return;
        // Auth must be a function, otherwise the expired token cannot be refreshed.
        client = ConnectWithLogin(() => LoginSubscribeAndGetTokenAsync(username, password));
        IDisposable observable = client
            .Child("users")
            .Child(CurrentUser.User.Uid)
            .Child(child)
            .AsObservable<T>()
            .Subscribe(data =>
            {
                try
                {
                    action?.Invoke(new Tuple<string, T>(data.Key, data.Object));
                }
                catch(Exception ex)
                {
                    EventManager.Instance.LogError(ex);
                    Unsubscribe<T>();
                }
            });
    }
    catch (Exception exc)
    {
        // Log error
        EventManager.Instance.LogError(exc);
    }
}

Maybe you can see what's wrong on my code?
Thanks a lot!

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