134 lines
4.5 KiB
Plaintext

@page "/registration"
@using System.Text.Json;
@using System.Collections.Generic
@using System.ComponentModel.DataAnnotations;
@inject HttpClient httpClient
@inject IToastService toastService
@inject NavigationManager navigation
<h1>Registro de Usuario</h1>
<EditForm Model="user" OnValidSubmit="RegistrarUsuario">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="InputUserName">Nombre de Usuario:</label>
<div class="col-md-4">
<InputText id="InputUserName" @bind-Value="user.UserName" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="InputEmail">Correo Electrónico:</label>
<div class="col-md-4">
<InputText id="InputEmail" @bind-Value="user.EmailAddress" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="InputPassword">Contraseña:</label>
<div class="col-md-4">
<InputText id="InputPassword" @bind-Value="user.Password" type="password" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="InputConfirmPassword">Confirmar Contraseña:</label>
<div class="col-md-4">
<InputText id="InputConfirmPassword" @bind-Value="confirmPassword" type="password" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="InputRole">Rol:</label>
<div class="col-md-4">
<InputSelect @bind-Value="user.Role" class="form-control">
@foreach (var role in roles)
{
<option value="@role.Name">@role.Name</option>
}
</InputSelect>
</div>
</div>
<br />
<button type="submit" class="btn btn-primary">Registrar</button>
<button type="button" class="btn btn-secondary" @onclick="Cancel">Cancelar</button>
</EditForm>
@code {
private User user = new User();
private string confirmPassword=string.Empty;
public List<Role> roles = new List<Role>(); // Reemplaza con tu lista de roles reales
private const int USERNAME_VALID_LENGTH = 8;
protected override async Task OnInitializedAsync()
{
roles = await GetAllRoles();
}
private async Task<List<Role>> GetAllRoles()
{
var response = await httpClient.GetFromJsonAsync<List<Role>>("/api/Account/GetAllRoles");
return response ?? new List<Role>();
}
private async Task RegistrarUsuario()
{
if (user.UserName.Length < USERNAME_VALID_LENGTH)
{
toastService.ShowError($"longitud de nombre minima: {USERNAME_VALID_LENGTH} caracteres. ¡Inténtalo de nuevo!");
}
else if (user.Password != confirmPassword)
{
toastService.ShowError("Las contraseñas no coinciden. ¡Inténtalo de nuevo!");
}
else if (string.IsNullOrEmpty(user.Role))
{
toastService.ShowWarning("Debes seleccionar un rol.");
}
else
{
await CreateUser();
}
}
public async Task CreateUser()
{
var requestContent = new StringContent(JsonSerializer.Serialize(user), System.Text.Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("/api/Authentication/register/", requestContent);
var errorResponse = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
toastService.ShowSuccess(errorResponse);
navigation.NavigateTo("/users");
}
else
{
toastService.ShowError(errorResponse);
}
}
public void Cancel()
{
navigation.NavigateTo("/users");
}
public class User
{
[Required(ErrorMessage = "El Username es un dato obligatorio")] public string UserName { get; set; } = string.Empty;
[EmailAddress, Required(ErrorMessage = "El correo electronico es un dato obligatorio")] public string EmailAddress { get; set; } = string.Empty;
[Required(ErrorMessage = "La contraseña es un dato obligatorio")] public string Password { get; set; } = string.Empty;
[Required(ErrorMessage = "El rol es un dato obligatorio")] public string Role { get; set; } = string.Empty;
}
public class Role
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string NormalizedName { get; set; } = string.Empty;
public string ConcurrencyStamp { get; set; } = string.Empty;
}
}