Disconnect when token expired
All checks were successful
Game Ideas build for PR / build_test (pull_request) Successful in 55s
All checks were successful
Game Ideas build for PR / build_test (pull_request) Successful in 55s
This commit is contained in:
@@ -14,8 +14,10 @@ public class AuthGateway(IHttpClientService httpClient,
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var token = await httpClient.PostAsync<TokenDto>(Endpoints.Auth.Login, userDto);
|
var token = await httpClient.PostAsync<TokenDto>(Endpoints.Auth.Login, userDto)
|
||||||
await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserAuthenticationAsync(token!.Token!);
|
?? throw new InvalidOperationException("Could not retrieve token");
|
||||||
|
|
||||||
|
await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserAuthenticationAsync(token);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
|
|||||||
@@ -5,13 +5,15 @@ using System.Text.Json;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Blazored.LocalStorage;
|
using Blazored.LocalStorage;
|
||||||
using GameIdeas.Shared.Constants;
|
using GameIdeas.Shared.Constants;
|
||||||
|
using Microsoft.AspNetCore.Components.Authorization;
|
||||||
|
|
||||||
namespace GameIdeas.BlazorApp.Services;
|
namespace GameIdeas.BlazorApp.Services;
|
||||||
|
|
||||||
public class HttpClientService(
|
public class HttpClientService(
|
||||||
IHttpClientFactory httpClientFactory,
|
IHttpClientFactory httpClientFactory,
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
ILocalStorageService localStorage) : IHttpClientService
|
ILocalStorageService localStorage,
|
||||||
|
AuthenticationStateProvider stateProvider) : IHttpClientService
|
||||||
{
|
{
|
||||||
private readonly HttpClient httpClient = httpClientFactory.CreateClient("GameIdeas.WebAPI");
|
private readonly HttpClient httpClient = httpClientFactory.CreateClient("GameIdeas.WebAPI");
|
||||||
private readonly ILogger<HttpClientService> logger = loggerFactory.CreateLogger<HttpClientService>();
|
private readonly ILogger<HttpClientService> logger = loggerFactory.CreateLogger<HttpClientService>();
|
||||||
@@ -141,6 +143,16 @@ public class HttpClientService(
|
|||||||
|
|
||||||
private async Task SetAuthorizationHeader()
|
private async Task SetAuthorizationHeader()
|
||||||
{
|
{
|
||||||
|
var expired = await localStorage.GetItemAsStringAsync(GlobalConstants.LS_EXPIRED_STORAGE_KEY);
|
||||||
|
|
||||||
|
if (expired == null
|
||||||
|
|| (DateTime.TryParse(expired, out DateTime expiration)
|
||||||
|
&& expiration < DateTime.UtcNow))
|
||||||
|
{
|
||||||
|
await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserLogoutAsync();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var token = await localStorage.GetItemAsStringAsync(GlobalConstants.LS_AUTH_STORAGE_KEY);
|
var token = await localStorage.GetItemAsStringAsync(GlobalConstants.LS_AUTH_STORAGE_KEY);
|
||||||
httpClient.DefaultRequestHeaders.Authorization =
|
httpClient.DefaultRequestHeaders.Authorization =
|
||||||
new AuthenticationHeaderValue("bearer", token);
|
new AuthenticationHeaderValue("bearer", token);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components.Authorization;
|
|||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.IdentityModel.Tokens.Jwt;
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
using GameIdeas.Shared.Constants;
|
using GameIdeas.Shared.Constants;
|
||||||
|
using GameIdeas.Shared.Dto;
|
||||||
|
|
||||||
namespace GameIdeas.BlazorApp.Services;
|
namespace GameIdeas.BlazorApp.Services;
|
||||||
|
|
||||||
@@ -31,9 +32,17 @@ public class JwtAuthenticationStateProvider(ILocalStorageService localStorage) :
|
|||||||
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
|
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task NotifyUserAuthenticationAsync(string token)
|
public async Task NotifyUserAuthenticationAsync(TokenDto token)
|
||||||
{
|
{
|
||||||
await localStorage.SetItemAsStringAsync(GlobalConstants.LS_AUTH_STORAGE_KEY, token);
|
if (token?.Token != null)
|
||||||
|
{
|
||||||
|
await localStorage.SetItemAsStringAsync(GlobalConstants.LS_AUTH_STORAGE_KEY, token.Token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token?.Expiration != null)
|
||||||
|
{
|
||||||
|
await localStorage.SetItemAsStringAsync(GlobalConstants.LS_EXPIRED_STORAGE_KEY, token.Expiration.Value.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,12 @@ public class GlobalConstants
|
|||||||
public const string MEMBER_NORMALIZED = "MEMBRE";
|
public const string MEMBER_NORMALIZED = "MEMBRE";
|
||||||
public const string ADMIN_MEMBER = $"{ADMINISTRATOR}, {MEMBER}";
|
public const string ADMIN_MEMBER = $"{ADMINISTRATOR}, {MEMBER}";
|
||||||
|
|
||||||
public const int JWT_DURATION_HOUR = 12;
|
public const int JWT_DURATION_HOUR = 168;
|
||||||
|
|
||||||
public const int NUMBER_PER_PAGE = 50;
|
public const int NUMBER_PER_PAGE = 50;
|
||||||
|
|
||||||
public const string LS_AUTH_STORAGE_KEY = "authToken";
|
public const string LS_AUTH_STORAGE_KEY = "authToken";
|
||||||
|
public const string LS_EXPIRED_STORAGE_KEY = "expiredToken";
|
||||||
|
|
||||||
public const int API_PORT = 8000;
|
public const int API_PORT = 8000;
|
||||||
public const string SUB_DOMAIN_NAME = "api-";
|
public const string SUB_DOMAIN_NAME = "api-";
|
||||||
|
|||||||
Reference in New Issue
Block a user