Correct bunch of issues (#36)
All checks were successful
Game Ideas deploy / build-test-deploy (push) Successful in 1m28s

Reviewed-on: #36
This commit was merged in pull request #36.
This commit is contained in:
2025-04-29 23:49:11 +02:00
parent d3d16493e6
commit f3c9e1d9da
37 changed files with 246 additions and 186 deletions

View File

@@ -5,13 +5,15 @@ using System.Text.Json;
using System.Text;
using Blazored.LocalStorage;
using GameIdeas.Shared.Constants;
using Microsoft.AspNetCore.Components.Authorization;
namespace GameIdeas.BlazorApp.Services;
public class HttpClientService(
IHttpClientFactory httpClientFactory,
ILoggerFactory loggerFactory,
ILocalStorageService localStorage) : IHttpClientService
ILocalStorageService localStorage,
AuthenticationStateProvider stateProvider) : IHttpClientService
{
private readonly HttpClient httpClient = httpClientFactory.CreateClient("GameIdeas.WebAPI");
private readonly ILogger<HttpClientService> logger = loggerFactory.CreateLogger<HttpClientService>();
@@ -141,6 +143,16 @@ public class HttpClientService(
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);
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("bearer", token);

View File

@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using GameIdeas.Shared.Constants;
using GameIdeas.Shared.Dto;
namespace GameIdeas.BlazorApp.Services;
@@ -31,9 +32,17 @@ public class JwtAuthenticationStateProvider(ILocalStorageService localStorage) :
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());
}