public override async Task <AuthenticationState> GetAuthenticationStateAsync() { var tokenObject = await _localStorage.GetItemAsync <TokenObject>("token"); if (tokenObject?.Token != null) { var now = DateTimeOffset.Now; if (now >= tokenObject.tokenExpirationTime.AddSeconds(30)) { if (await _authenticationService.Value.RefreshToken()) { tokenObject = await _localStorage.GetItemAsync <TokenObject>("token"); } } var claimsPrincipal = _jwtDecoder.GetClaimsPrincipalForJwtToken(tokenObject.Token, out _); if (claimsPrincipal != null) { return(new AuthenticationState(claimsPrincipal)); } } return(_anonymous); }
public async Task <AuthResponseDto> Login(UserForAuthenticationDto userForAuthentication) { var authResult = await _client.Value.PostAsJsonAsync("/api/auth/login", userForAuthentication); var result = await authResult.Content.ReadFromJsonAsync <AuthResponseDto>(); if (!authResult.IsSuccessStatusCode || !result.Succeeded) { return(result); } await SaveToken(result); var claimsPrincipal = _jwtDecoder.GetClaimsPrincipalForJwtToken(result.Tokens.Token, out SecurityToken _); ((AuthStateProvider)_authStateProvider).NotifyUserAuthentication(claimsPrincipal); return(result); }