varianta proiect care merge
This commit is contained in:
@@ -5,19 +5,19 @@ namespace HealthcareManagerUI.Services.Authentication;
|
||||
// In AuthenticationService.cs
|
||||
public class AuthenticationService : IAuthenticationService
|
||||
{
|
||||
private readonly IHttpService _httpService;
|
||||
private readonly IRequestHttpService _requestHttpService;
|
||||
private readonly string _apiUrl = "http://localhost:5151/api";
|
||||
|
||||
public AuthenticationService(IHttpService httpService)
|
||||
public AuthenticationService(IRequestHttpService requestHttpService)
|
||||
{
|
||||
_httpService = httpService;
|
||||
_requestHttpService = requestHttpService;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> LoginDoctor(UserLoginModel userLoginModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/login", userLoginModel);
|
||||
var response = await _requestHttpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/login", userLoginModel);
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -35,7 +35,7 @@ public class AuthenticationService : IAuthenticationService
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/login", userLoginModel);
|
||||
var response = await _requestHttpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/login", userLoginModel);
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -52,7 +52,7 @@ public class AuthenticationService : IAuthenticationService
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/register", userRegistrationModel);
|
||||
var response = await _requestHttpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/register", userRegistrationModel);
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -77,7 +77,7 @@ public class AuthenticationService : IAuthenticationService
|
||||
|
||||
try
|
||||
{
|
||||
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/register", userRegisterModel);
|
||||
var response = await _requestHttpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/register", userRegisterModel);
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -95,7 +95,7 @@ public class AuthenticationService : IAuthenticationService
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/reset_password", userResetPasswordModel);
|
||||
var response = await _requestHttpService.PostAsync<BaseResponse>($"{_apiUrl}/Doctors/reset_password", userResetPasswordModel);
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -113,7 +113,7 @@ public class AuthenticationService : IAuthenticationService
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/reset_password", userResetPasswordModel);
|
||||
var response = await _requestHttpService.PostAsync<BaseResponse>($"{_apiUrl}/Patients/reset_password", userResetPasswordModel);
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace HealthcareManagerUI.Services.Http;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public interface IRequestHttpService
|
||||
{
|
||||
Task<T> GetAsync<T>(string uri, IDictionary<string, string> headers = null);
|
||||
Task<T> GetByIdAsync<T>(string uri, int id, IDictionary<string, string> headers = null);
|
||||
Task<T> PostAsync<T>(string uri, object data, IDictionary<string, string> headers = null);
|
||||
Task<T> PutAsync<T>(string uri, int id, object data, IDictionary<string, string> headers = null);
|
||||
Task DeleteAsync(string uri, int id, IDictionary<string, string> headers = null);
|
||||
}
|
||||
+2
-2
@@ -3,11 +3,11 @@ using System.Text.Json;
|
||||
|
||||
namespace HealthcareManagerUI.Services.Http;
|
||||
|
||||
public class HttpService : IHttpService
|
||||
public class RequestHttpService : IRequestHttpService
|
||||
{
|
||||
private readonly string _apiKey;
|
||||
|
||||
public HttpService(IConfiguration configuration)
|
||||
public RequestHttpService(IConfiguration configuration)
|
||||
{
|
||||
_apiKey = configuration.GetValue<string>("ApiKey") ?? "NoKey";
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user