Files
FACULTATE-HEALTHCARE_MANAGER/frontend/Components/Pages/LoginPage.razor
T

113 lines
4.2 KiB
Plaintext

@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}");
}
}
}