varianta proiect care merge

This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 14:23:44 +03:00
parent 370bbdedcd
commit 897193c865
333 changed files with 1090 additions and 98769 deletions
@@ -0,0 +1,10 @@
namespace HealthcareManagerUI.Services.TokenService;
using System.Threading.Tasks;
public interface ITokenService
{
Task<string> GetTokenAsync();
Task SaveTokenAsync(string token);
Task RefreshTokenAsync();
}
@@ -0,0 +1,45 @@
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Blazored.LocalStorage;
namespace HealthcareManagerUI.Services.TokenService;
public class TokenService : ITokenService
{
private readonly ILocalStorageService _localStorage;
private readonly HttpClient _httpClient;
private const string TokenKey = "";
public TokenService(ILocalStorageService localStorage, HttpClient httpClient)
{
_localStorage = localStorage;
_httpClient = httpClient;
}
public async Task<string> GetTokenAsync()
{
return await _localStorage.GetItemAsStringAsync(TokenKey);
}
public async Task SaveTokenAsync(string token)
{
await _localStorage.SetItemAsStringAsync(TokenKey, token);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
public async Task RefreshTokenAsync()
{
var token = await GetTokenAsync();
var jsonRequest = JsonSerializer.Serialize(new { token = token });
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("http://localhost:5151/api/Authorization/refresh_token", content);
if (response.IsSuccessStatusCode)
{
var newToken = await response.Content.ReadAsStringAsync();
await SaveTokenAsync(newToken);
}
}
}