Correct bunch of issues (#36)
All checks were successful
Game Ideas deploy / build-test-deploy (push) Successful in 1m28s
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user