finalizare 1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-05-21 12:10:53 +03:00
parent f7795f7519
commit 1cc1d34003
11268 changed files with 2102399 additions and 10909 deletions
@@ -1,104 +1,98 @@
using HealthcareManagerUiWebAssem.Models;
using HealthcareManagerUiWebAssem.Services.Http;
using System.IdentityModel.Tokens.Jwt;
using HealthcareManagerUiWebAssem.Models;
using System.Net.Http.Json;
using System.Security.Claims;
using System.Text.Json;
using Blazored.LocalStorage;
using HealthcareManagerUiWebAssem.Services.RequestHttp;
using Microsoft.AspNetCore.Components.Authorization;
namespace HealthcareManagerUiWebAssem.Services.Authentication;
public class AuthenticationService : IAuthenticationService
public class AuthenticationService(
IRequestHttpService requestHttpService,
ILocalStorageService localStorageService,
AuthenticationStateProvider authenticationStateProvider)
: IAuthenticationService
{
private readonly IRequestHttpService _requestHttpService;
public AuthenticationService(IRequestHttpService requestHttpService)
public async Task<ApiResponse<string>> Login(UserLoginModel loginModel)
{
_requestHttpService = requestHttpService;
var response = await requestHttpService.PostAsync("/api/Authorization/login", loginModel);
var responseContent = await response.Content.ReadAsStringAsync();
var loginResponse = JsonSerializer.Deserialize<ApiResponse<string>>(responseContent,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (response.IsSuccessStatusCode) await localStorageService.SetItemAsync("authToken", loginResponse.Data);
return loginResponse;
}
public async Task<BaseResponse> Login(UserLoginModel userLoginModel)
public async Task<ApiResponse<string>> Register(UserRegisterModel registerModel)
{
try
{
var response = await _requestHttpService.PostAsync("/Authorization/login", userLoginModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = HttpStatusCodes.InternalServerError,
Data = null,
Message = ex.Message
};
}
var response = await requestHttpService.PostAsync("/api/Authorization/register", registerModel);
var responseContent = await response.Content.ReadAsStringAsync();
var registerResponse = JsonSerializer.Deserialize<ApiResponse<string>>(responseContent,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
return registerResponse;
}
public async Task<BaseResponse> RegisterDoctor(UserRegisterModel userRegistrationModel)
public async Task<ApiResponse<string>> ResetJwt(string token)
{
try
{
var response = await _requestHttpService.PostAsync("/Doctors/register", userRegistrationModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = HttpStatusCodes.InternalServerError,
Data = null,
Message = ex.Message
};
}
}
public async Task<BaseResponse> RegisterPatient(UserRegisterModel userRegistrationModel)
{
try
{
var response = await _requestHttpService.PostAsync("/Patients/register", userRegistrationModel);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = HttpStatusCodes.InternalServerError,
Data = null,
Message = ex.Message
};
}
var requestBody = new { token = token };
var response = await requestHttpService.PostAsync("/api/Authorization/refresh_token", requestBody);
var responseContent = await response.Content.ReadAsStringAsync();
var resetJwtResponse = JsonSerializer.Deserialize<ApiResponse<string>>(responseContent,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (response.IsSuccessStatusCode) await localStorageService.SetItemAsync("authToken", resetJwtResponse.Data);
return resetJwtResponse;
}
public async Task<BaseResponse> ResetPassword(UserLoginModel dto)
public async Task<ApiResponse<object>> ResetPassword(ResetPasswordModel resetPasswordModel)
{
try
{
var response = await _requestHttpService.PostAsync("/Authorization/reset_password", dto);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = HttpStatusCodes.InternalServerError,
Data = null,
Message = ex.Message
};
}
var requestBody = new { email = resetPasswordModel.Email, password = resetPasswordModel.Password };
var response = await requestHttpService.PostAsync("/api/Authorization/reset_password", requestBody);
var responseContent = await response.Content.ReadAsStringAsync();
var resetPasswordResponse = JsonSerializer.Deserialize<ApiResponse<object>>(responseContent,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
return resetPasswordResponse;
}
public async Task<BaseResponse> RefreshToken(TokenRefreshModel dto)
public async Task<UserInformation?> GetUserInformation()
{
try
var token = await GetAuthToken();
if (string.IsNullOrEmpty(token)) return null;
var handler = new JwtSecurityTokenHandler();
var jwtToken = handler.ReadJwtToken(token);
var name = jwtToken.Claims.FirstOrDefault(c => c.Type == "unique_name")?.Value;
var id = jwtToken.Claims.FirstOrDefault(c => c.Type == "nameid")?.Value;
var role = jwtToken.Claims.FirstOrDefault(c => c.Type == "role")?.Value;
return new UserInformation
{
var response = await _requestHttpService.PostAsync("/Authorization/refresh_token", dto);
return response;
}
catch (Exception ex)
{
return new BaseResponse
{
StatusCode = HttpStatusCodes.InternalServerError,
Data = null,
Message = ex.Message
};
}
Name = name,
Id = Guid.Parse(id),
Role = role
};
}
public async Task Logout()
{
await localStorageService.RemoveItemAsync("authToken");
((CustomAuthenticationStateProvider)authenticationStateProvider).NotifyUserLogout();
}
public async Task<string> GetAuthToken()
{
return await localStorageService.GetItemAsync<string>("authToken");
}
public async Task RemoveAuthToken()
{
await localStorageService.RemoveItemAsync("authToken");
}
}