migrare UI in WebAssembly + integrare Serviciu stocare info pe web + WebToken

This commit is contained in:
andrei-mihnea-cerbu
2024-04-10 06:57:48 +03:00
parent 9977a9b5f5
commit adba50e70b
1915 changed files with 16113 additions and 99991 deletions
@@ -0,0 +1,16 @@
@inherits LayoutComponentBase
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/AuthLayout.css"/>
</head>
<body>
<div class="background"></div>
<div class="container">
@Body
</div>
</body>
</html>
@@ -0,0 +1,11 @@
@if (!string.IsNullOrEmpty(ErrorMessage))
{
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Error:</strong> @ErrorMessage
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
}
@code {
[Parameter] public string ErrorMessage { get; set; }
}
@@ -0,0 +1,15 @@
@page "/"
@layout AuthLayout
<head>
<title>Choose Role</title>
</head>
<link rel="stylesheet" href="bootstrap/dist/css/bootstrap.min.css"/>
<div class="text-center mt-5 centered-menu">
<h1 class="landingTitle">Welcome to Healthcare Manager</h1>
<p class="landingSubtitle">Please select your role:</p>
<div class="button-container">
<NavLink class="btn btn-primary m-2" href="/login/doctor">I'm a Doctor</NavLink>
<NavLink class="btn btn-secondary m-2" href="/login/patient">I'm a Patient</NavLink>
</div>
</div>
@@ -0,0 +1,28 @@
@page "/dashboard/{Role}"
@using HealthcareManagerUiWebAssem.Services.User
@layout AuthLayout
@inject UserService UserService
<head>
<title>Dashboard</title>
</head>
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css"/>
<h3>login success</h3>
<h1>@displayMessage</h1>
<div class="position-relative">
<NavLink class="btn btn-primary m-2 position-absolute top-0 end-0" href="@($"/my-profile/{Role}")">My Profile</NavLink>
</div>
@code {
[Parameter] public string Role { get; set; }
private string displayMessage;
protected override void OnInitialized()
{
var userId = UserService.UserId;
displayMessage = $"Login success for {Role} with ID: {userId}";
}
}
+113
View File
@@ -0,0 +1,113 @@
@page "/login/{role}"
@using System.Text.Json
@using HealthcareManagerUiWebAssem.Models
@using HealthcareManagerUiWebAssem.Services.Authentication
@using HealthcareManagerUiWebAssem.Services.User
@using HealthcareManagerUiWebAssem.Services.UserSessionInformation
@layout AuthLayout
@inject IAuthenticationService AuthenticationService
@inject IUserSessionInformation UserSessionInformation;
@inject NavigationManager NavigationManager
@inject UserService UserService
<head>
<title>Login</title>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<link rel="stylesheet" href="/LoginPage.css"/>
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css"/>
<EditForm Model="userLoginModel" OnValidSubmit="@HandleLogin" FormName="LoginForm" class="login-form container mt-5">
<DataAnnotationsValidator/>
<ValidationSummary/>
<div class="card shadow-lg">
<div class="card-body">
<h3 class="card-title text-center mb-3">Login</h3>
<div class="form-group mb-3">
<InputText id="email" class="form-control" placeholder="Email" @bind-Value="userLoginModel!.Email"></InputText>
</div>
<div class="form-group mb-3">
<InputText id="password" class="form-control" type="password" placeholder="Password" @bind-Value="userLoginModel!.Password"></InputText>
</div>
<button type="submit" class="btn btn-primary w-100 mb-3">Log In</button>
<div class="button-container">
<NavLink class="btn btn-secondary m-2" href="@($"/register/{Role}")">Register</NavLink>
<NavLink class="btn btn-secondary m-2" href="@($"/reset-password/{Role}")">Reset Password</NavLink>
</div>
</div>
</div>
</EditForm>
<AlertMessage ErrorMessage="@errorMessage"/>
@code {
[SupplyParameterFromForm] public UserLoginModel? userLoginModel { get; set; }
private BaseResponse? loginResponse;
protected override void OnInitialized()
{
userLoginModel ??= new UserLoginModel();
}
[Parameter]
public string Role { get; set; }
private string errorMessage { get; set; }
private bool loginSuccesful { get; set; }
private void ClearErrorMessage()
{
errorMessage = string.Empty;
}
private async Task HandleLogin()
{
userLoginModel.UserType = Role;
loginResponse = await AuthenticationService.Login(userLoginModel); // Store response instead of immediately processing
if (loginResponse.StatusCode < 200 || loginResponse.StatusCode > 299)
{
errorMessage = loginResponse?.Message ?? "An error occurred."; // Handle error states immediately if required
}
else
{
var userId = Guid.Empty;
var authToken = "";
Console.WriteLine("Printing headers");
foreach (var (key, value) in loginResponse.Headers)
{
Console.WriteLine($"{key} => {value}");
}
if (loginResponse.Headers.TryGetValue("Authorization", out var token))
{
authToken = token;
}
if (Role.Equals("doctor", StringComparison.OrdinalIgnoreCase))
{
var doctor = JsonSerializer.Deserialize<Doctor>(
loginResponse.Data,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
await UserSessionInformation.SaveUserInformationAsync(doctor.Id, Role, doctor.Email, doctor.Name, authToken);
userId = doctor.Id;
}
else if (Role.Equals("patient", StringComparison.OrdinalIgnoreCase))
{
var patient = JsonSerializer.Deserialize<Patient>(
loginResponse.Data,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
await UserSessionInformation.SaveUserInformationAsync(patient.Id, Role, patient.Email, patient.Name, authToken);
userId = patient.Id;
}
loginResponse = null;
UserService.SetUserId(userId);
NavigationManager.NavigateTo($"/dashboard/{Role}");
}
}
}
@@ -0,0 +1,123 @@
@page "/my-profile/{Role}"
@using HealthcareManagerUiWebAssem.Models
@using HealthcareManagerUiWebAssem.Services.Authentication
@using HealthcareManagerUiWebAssem.Services.Profile
@using HealthcareManagerUiWebAssem.Services.User
@using HealthcareManagerUiWebAssem.Services.UserSessionInformation
@layout AuthLayout
@inject IUserSessionInformation UserSessionInformation;
@inject IAuthenticationService AuthenticationService;
@inject IProfileService ProfileService
@inject UserService UserService
@inject NavigationManager NavigationManager
<head>
<title>My Profile</title>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<link rel="stylesheet" href="/LoginPage.css"/>
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css"/>
<h3>My Profile</h3>
@if (profile != null)
{
<EditForm Model="profile" OnValidSubmit="@HandleUpdateProfile" FormName="UpdateProfileForm" class="login-form container mt-5">
<DataAnnotationsValidator/>
<ValidationSummary/>
<div class="card shadow-lg">
<div class="card-body">
<div class="form-group mb-3">
<label for="name">Name:</label>
<InputText id="name" class="form-control" @bind-Value="@profile!.Name"></InputText>
</div>
@if (Role.Equals("doctor", StringComparison.OrdinalIgnoreCase))
{
<div class="form-group mb-3">
<label for="description">Description:</label>
<InputText id="description" class="form-control" @bind-Value="@profile!.Description"></InputText>
</div>
}
<div class="form-group mb-3">
<label for="email">Email:</label>
<InputText id="email" class="form-control" @bind-Value="@profile!.Email"></InputText>
</div>
<div class="form-group mb-3">
<label for="password">Password:</label>
<InputText id="password" class="form-control" type="password" @bind-Value="@profile!.Password"></InputText>
</div>
<button type="submit" class="btn btn-primary">Update</button>
<button type="button" class="btn btn-danger" @onclick="@HandleDeleteProfile">Delete</button>
</div>
</div>
</EditForm>
<AlertMessage ErrorMessage="@errorMessage"/>
}
@code {
[SupplyParameterFromForm] public UserUpdateProfileModel? profile { get; set; }
[Parameter] public string Role { get; set; }
private string errorMessage;
protected override async Task OnInitializedAsync()
{
var tokenDto = new TokenRefreshModel(await UserSessionInformation.GetTokenAsync());
var refreshResult = await AuthenticationService.RefreshToken(tokenDto);
if (refreshResult.StatusCode < HttpStatusCodes.BadRequest)
{
NavigationManager.NavigateTo("/");
}
profile ??= await UserService.InitializeProfile(Role, ProfileService);
var userId = await UserSessionInformation.GetIdAsync();
}
private async Task HandleUpdateProfile()
{
profile.Id = UserService.UserId;
var response = Role switch
{
"doctor" => await ProfileService.UpdateDoctorProfile(profile),
"patient" => await ProfileService.UpdatePatientProfile(profile),
_ => null
};
if (response.StatusCode >= 200 && response.StatusCode <= 299)
{
NavigationManager.NavigateTo($"/my-profile/{Role}");
}
else
{
errorMessage = response?.Message ?? "An error occurred.";
}
}
private async Task HandleDeleteProfile()
{
var response = Role switch
{
"doctor" => await ProfileService.DeleteDoctorProfile(UserService.UserId),
"patient" => await ProfileService.DeletePatientProfile(UserService.UserId),
_ => null
};
if (response.StatusCode >= 200 && response.StatusCode <= 299)
{
NavigationManager.NavigateTo($"/login/{Role}");
}
else
{
errorMessage = response?.Message ?? "An error occurred.";
}
}
}
@@ -0,0 +1,83 @@
@page "/register/{role}"
@using HealthcareManagerUiWebAssem.Models
@using HealthcareManagerUiWebAssem.Services.Authentication
@layout AuthLayout
@inject IAuthenticationService AuthenticationService
@inject NavigationManager NavigationManager
<head>
<title>Register</title>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<link rel="stylesheet" href="/LoginPage.css"/>
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css"/>
<EditForm Model="userRegisterModel" OnValidSubmit="@HandleRegister" FormName="RegisterForm" class="login-form container mt-5">
<DataAnnotationsValidator/>
<ValidationSummary/>
<div class="card shadow-lg">
<div class="card-body">
<h3 class="card-title text-center mb-3">Register</h3>
<div class="form-group mb-3">
<InputText id="name" class="form-control" placeholder="Name" @bind-Value="userRegisterModel!.Name"></InputText>
</div>
@if (Role.Equals("doctor", StringComparison.OrdinalIgnoreCase))
{
<div class="form-group mb-3">
<InputText id="description" class="form-control" placeholder="Description" @bind-Value="userRegisterModel!.Description"></InputText>
</div>
}
<div class="form-group mb-3">
<InputText id="email" class="form-control" placeholder="Email" @bind-Value="userRegisterModel!.Email"></InputText>
</div>
<div class="form-group mb-3">
<InputText id="password" class="form-control" type="password" placeholder="Password" @bind-Value="userRegisterModel!.Password"></InputText>
</div>
<div class="button-container">
<button type="submit" class="btn btn-primary w-100 mb-3">Register</button>
</div>
</div>
</div>
</EditForm>
<AlertMessage ErrorMessage="@errorMessage"/>
@code {
[SupplyParameterFromForm] public UserRegisterModel? userRegisterModel { get; set; }
protected override void OnInitialized()
{
userRegisterModel ??= new UserRegisterModel();
}
[Parameter] public string Role { get; set; }
private string errorMessage { get; set; }
private void ClearErrorMessage()
{
errorMessage = string.Empty;
}
private async Task HandleRegister()
{
var response = Role switch
{
"doctor" => await AuthenticationService.RegisterDoctor(userRegisterModel),
"patient" => await AuthenticationService.RegisterPatient(userRegisterModel),
_ => null
};
if (response.StatusCode >= 200 && response.StatusCode <= 399)
{
NavigationManager.NavigateTo($"/login/{Role}");
}
else
{
errorMessage = response.Message;
}
}
}
@@ -0,0 +1,70 @@
@page "/reset-password/{role}"
@using HealthcareManagerUiWebAssem.Models
@using HealthcareManagerUiWebAssem.Services.Authentication
@layout AuthLayout
@inject IAuthenticationService AuthenticationService
@inject NavigationManager NavigationManager
<head>
<title>Reset your password</title>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<link rel="stylesheet" href="/LoginPage.css"/>
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css"/>
<EditForm Model="userResetPasswordModel" OnValidSubmit="@HandleResetPassword" FormName="ResetPasswordForm" class="login-form container mt-5">
<DataAnnotationsValidator/>
<ValidationSummary/>
<div class="card shadow-lg">
<div class="card-body">
<h3 class="card-title text-center mb-3">Register</h3>
<div class="form-group mb-3">
<InputText id="email" class="form-control" placeholder="Email" @bind-Value="userResetPasswordModel!.Email"></InputText>
</div>
<div class="form-group mb-3">
<InputText id="password" class="form-control" type="password" placeholder="New Password" @bind-Value="userResetPasswordModel!.Password"></InputText>
</div>
<div class="button-container">
<button type="submit" class="btn btn-primary w-100 mb-3">Reset Password</button>
</div>
</div>
</div>
</EditForm>
<AlertMessage ErrorMessage="@errorMessage"/>
@code {
[SupplyParameterFromForm] public UserLoginModel? userResetPasswordModel { get; set; }
protected override void OnInitialized()
{
userResetPasswordModel ??= new UserLoginModel();
}
[Parameter] public string Role { get; set; }
private string errorMessage { get; set; }
private void ClearErrorMessage()
{
errorMessage = string.Empty;
}
private async Task HandleResetPassword()
{
userResetPasswordModel.UserType = Role;
var response = await AuthenticationService.ResetPassword(userResetPasswordModel);
if (response.StatusCode >= 200 && response.StatusCode <= 399)
{
NavigationManager.NavigateTo($"/login/{Role}");
}
else
{
errorMessage = response.Message;
}
}
}