finalizare 1.0
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
@page "/login"
|
||||
@inject IAuthenticationService AuthenticationService
|
||||
@inject NavigationManager Navigation
|
||||
|
||||
@layout AuthLayout
|
||||
<h3>Login</h3>
|
||||
|
||||
@if (!string.IsNullOrEmpty(_errorMessage))
|
||||
{
|
||||
<div class="alert alert-danger mt-2">@_errorMessage</div>
|
||||
}
|
||||
|
||||
<EditForm Model="_loginModel" OnValidSubmit="HandleLogin">
|
||||
<div class="form-group">
|
||||
<label>Email:</label>
|
||||
<InputText class="form-control" @bind-Value="_loginModel.Email"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Password:</label>
|
||||
<InputText class="form-control" @bind-Value="_loginModel.Password" type="password"/>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Login</button>
|
||||
</EditForm>
|
||||
|
||||
@code {
|
||||
private readonly UserLoginModel _loginModel = new();
|
||||
private string _errorMessage = string.Empty;
|
||||
|
||||
private async Task HandleLogin()
|
||||
{
|
||||
_errorMessage = string.Empty;
|
||||
var response = await AuthenticationService.Login(_loginModel);
|
||||
|
||||
if (response.StatusCode == 200)
|
||||
{
|
||||
var token = await AuthenticationService.GetAuthToken();
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadJwtToken(token);
|
||||
var role = jwtToken.Claims.FirstOrDefault(c => c.Type == "role")?.Value;
|
||||
|
||||
switch (role)
|
||||
{
|
||||
case UserRoles.Admin:
|
||||
Navigation.NavigateTo("/admin/dashboard", true);
|
||||
break;
|
||||
case UserRoles.Doctor:
|
||||
Navigation.NavigateTo("/doctor/dashboard", true);
|
||||
break;
|
||||
case UserRoles.Patient:
|
||||
Navigation.NavigateTo("/patient/dashboard", true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorMessage = response.Message; // Display error message
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
var token = await AuthenticationService.GetAuthToken();
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadJwtToken(token);
|
||||
var role = jwtToken.Claims.FirstOrDefault(c => c.Type == "role")?.Value;
|
||||
|
||||
switch (role)
|
||||
{
|
||||
case UserRoles.Admin:
|
||||
Navigation.NavigateTo("/admin/dashboard", true);
|
||||
break;
|
||||
case UserRoles.Doctor:
|
||||
Navigation.NavigateTo("/doctor/dashboard", true);
|
||||
break;
|
||||
case UserRoles.Patient:
|
||||
Navigation.NavigateTo("/patient/dashboard", true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user