I want check exp JWT Token everytime when user execute some action. Now my verify JWT Token execute when user move between pages and if token is expired then user see login page, because i check jwt token in MainLayour.razor in OnAfterRenderAsync
protected override async Task OnAfterRenderAsync(bool firstRender)
{
//if (firstRender)
await _authProvider.GetAuthenticationStateAsync();
}
I think is a bad idea
GetAuthenticationStateAsync:
var anonymousState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
var token = await _localStorageService.GetItemAsync<string>("token");
var payload = String.IsNullOrEmpty(token)
? new ClaimsIdentity()
: new ClaimsIdentity(ParseClaimsFromJwt(token), "jwt");
var exp = await _localStorageService.GetItemAsync<string>("exp");
if (String.IsNullOrEmpty(token))
return anonymousState;
var datetime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(payload.Claims.First(x => x.Type == "exp").Value));
if (datetime.UtcDateTime <= DateTime.UtcNow)
{
await LogoutAsync();
return anonymousState;
}
and usage LogoutAsync() after verify exp time i think is too bad idea. How can i implement verify JWT after user actions or after response 401 from my server ?
Problem occurs when token expired and user is still on page, but user switches between tab data on this page. For example: Page shows tickets data and user can switch between Active, Close ticket then user see "No results" text, but I want show login page.