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

use Token in SessionStorage/LocalStorage from your maui blazor code #70

Open
MOAnfaDaniel922 opened this issue Mar 20, 2023 · 1 comment

Comments

@MOAnfaDaniel922
Copy link

I have made some updates to your code in the class Auth0AuthenticationStateProvider to allow users to remain logged in after closing their browser in the maui blazor application.Specifically, I updated the method GetAuthenticationStateAsync to include the functionality of storing the token in localstorage/sessionstorage. Here is the updated code for your reference:

`public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
    string jsonWebToken = null;
    if (await _localStorageService.ContainKeyAsync(authEntrykey)) {
        jsonWebToken = await _sessionStorageService.GetItemAsStringAsync(authEntrykey);
    }
                
    if (jsonWebToken == null)
    {
        return new AuthenticationState(currentUser);
    }
    else
    {
        return new AuthenticationState(new ClaimsPrincipal(currentUser));
    }
}

`
In addition, I updated the LogIn method as follows to ensure the user is redirected to the correct page after logging in:

`private async Task LogIn(MouseEventArgs args)
{
    await ((Auth0AuthenticationStateProvider)AuthenticationStateProvider).LogInAsync();
    var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
    
    if (IsConnected(authenticationState) && UserIsAdmin(authenticationState))
    {
        _defaultRedirectUrl = "/dashboard";
    }
}
`

and it doesn't seem to be working. Would it be possible for you to retest and correct the code updated so that all users can benefit from this new functionality? Thank you for your assistance.

@MOAnfaDaniel922 MOAnfaDaniel922 changed the title To add /read token in SessionStorage/LocalStorage from your maui blazor code use Token in SessionStorage/LocalStorage from your maui blazor code Mar 31, 2023
@grantcolley
Copy link
Owner

@MOAnfaDaniel922, thanks for the feedback and your code snippets. Unfortunately because of time constraints I have not been able to focus on the MAUI project as I hoped and have set it aside for the time being. I do intend getting back to it at some point and will pick up your suggestions then.

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

2 participants