finalizare 1.0
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
@page "/register"
|
||||
@inject NavigationManager Navigation
|
||||
@inject IAuthenticationService AuthenticationService
|
||||
@layout AuthLayout
|
||||
|
||||
<h3>Register</h3>
|
||||
|
||||
@if (!string.IsNullOrEmpty(_errorMessage))
|
||||
{
|
||||
<div class="alert alert-danger mt-2">@_errorMessage</div>
|
||||
}
|
||||
|
||||
|
||||
<EditForm Model="_registerModel" OnValidSubmit="HandleRegister">
|
||||
<div class="form-group">
|
||||
<label>Name:</label>
|
||||
<InputText class="form-control" @bind-Value="_registerModel.Name"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Email:</label>
|
||||
<InputText class="form-control" @bind-Value="_registerModel.Email"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Password:</label>
|
||||
<InputText class="form-control" @bind-Value="_registerModel.Password" type="password"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Role:</label>
|
||||
<InputSelect class="form-control" @bind-Value="_registerModel.Role">
|
||||
<option value="">Select a role</option>
|
||||
<option value="Doctor">Doctor</option>
|
||||
<option value="Patient">Patient</option>
|
||||
</InputSelect>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Register</button>
|
||||
</EditForm>
|
||||
|
||||
@code {
|
||||
private readonly UserRegisterModel _registerModel = new();
|
||||
private string _errorMessage = string.Empty;
|
||||
|
||||
private async Task HandleRegister()
|
||||
{
|
||||
_errorMessage = string.Empty;
|
||||
var response = await AuthenticationService.Register(_registerModel);
|
||||
|
||||
if (response.StatusCode == 201)
|
||||
{
|
||||
// Registration successful, navigate to login page
|
||||
Navigation.NavigateTo("/login", true);
|
||||
}
|
||||
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