use Token in SessionStorage/LocalStorage from your maui blazor code
MOAnfaDaniel922 opened this issue · comments
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, 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.