Ajout Register.razor. Désactivation validation formulaire. Pas d'appel au service lors de l'action Register

This commit is contained in:
2026-01-25 16:10:59 +01:00
parent 52381dcb89
commit fac6f41910
6 changed files with 74 additions and 11 deletions

View File

@@ -12,7 +12,7 @@
<div class="d-flex justify-content-center align-items-center"> <div class="d-flex justify-content-center align-items-center">
<div class="col-md-4 p-5 shadow-sm border rounded-3"> <div class="col-md-4 p-5 shadow-sm border rounded-3">
<h2 class="text-center mb-4 text-primary">Login Form</h2> <h2 class="text-center mb-4 text-primary">Login Form</h2>
<EditForm Model="userLogin" OnValidSubmit="Authenticate"> <EditForm Model="userLogin" OnValidSubmit="Authenticate" FormName="LoginForm">
<DataAnnotationsValidator /> <DataAnnotationsValidator />
<div class="mb-3"> <div class="mb-3">
<label for="email">User Name</label> <label for="email">User Name</label>
@@ -50,6 +50,8 @@
private async Task Authenticate() private async Task Authenticate()
{ {
Console.WriteLine("***");
Console.WriteLine(userLogin.UserName);
var result = await AuthService.Login(userLogin.UserName, userLogin.Password); var result = await AuthService.Login(userLogin.UserName, userLogin.Password);
if (result.Success) if (result.Success)
{ {

View File

@@ -0,0 +1,57 @@
@page "/register"
@using BlazorPolicyAuth.Models.ViewModels
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
@inject IAuthService AuthService
<title>Register</title>
<EditForm Model="user" OnValidSubmit="HandleRegistration" FormName="registerForm">
<DataAnnotationsValidator/>
<div class="d-flex justify-content-center align-items-center">
<div class="col-md-4 p-5 shadow-sm border rounded-3">
<h2 class="text-center mb-4 text-primary">Register Form</h2>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<InputText type="email" @bind-Value="user.Email" class="form-control border border-primary" id="email" aria-describedby="email"/>
<ValidationMessage For="@(() => user.Email)"/>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<InputText type="password" @bind-Value="user.Password" class="form-control border border-primary" id="password" aria-describedby="password"/>
<ValidationMessage For="@(() => user.Password)"/>
</div>
<div class="mb-3">
<label for="confirmPassword" class="form-label">Confirm password</label>
<InputText type="password" @bind-Value="user.ConfirmPassword" class="form-control border border-primary" id="confirmPassword" aria-describedby="confirmPassword"/>
<ValidationMessage For="@(() => user.ConfirmPassword)"/>
</div>
<div class="d-grid">
<button class="btn btn-primary" type="submit">Register</button>
</div>
<div class="mt-3">
<p class="mb-0 text-center">You have an account? <a href="login" class="text-primary fw-bold">Sign In</a></p>
</div>
</div>
</div>
</EditForm>
<div class="text-danger">
<span>@errorMessage</span>
</div>
@code {
UserRegister user = new();
private string message = string.Empty;
private string messageCssClass = string.Empty;
private string errorMessage = string.Empty;
async void HandleRegistration()
{
var result = await AuthService.Register(user.Email, user.Password);
message = result.Message;
messageCssClass = result.Success ? "text-success" : "text-danger";
}
}

View File

@@ -4,8 +4,9 @@ namespace BlazorPolicyAuth.Models.ViewModels;
public class UserLogin public class UserLogin
{ {
[Required, EmailAddress] //[Required, EmailAddress]
//[Required]
public string UserName { get; set; } = string.Empty; public string UserName { get; set; } = string.Empty;
[Required] //[Required]
public string Password { get; set; } public string Password { get; set; }
} }

View File

@@ -4,9 +4,9 @@ namespace BlazorPolicyAuth.Models.ViewModels
{ {
public class UserRegister public class UserRegister
{ {
[Required, EmailAddress] //[Required, EmailAddress]
public string Email { get; set; } = string.Empty; public string Email { get; set; } = string.Empty;
[Required, StringLength(100, MinimumLength = 5)] //[Required, StringLength(100, MinimumLength = 5)]
public string Password { get; set; } = string.Empty; public string Password { get; set; } = string.Empty;
[Compare("Password", ErrorMessage = "The password do not match.")] [Compare("Password", ErrorMessage = "The password do not match.")]
public string ConfirmPassword { get; set; } = string.Empty; public string ConfirmPassword { get; set; } = string.Empty;

View File

@@ -25,17 +25,20 @@ public class AuthService : IAuthService
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
} }
public async Task<ServiceResponse<int>> Register(UserAccount user, string password) public async Task<ServiceResponse<int>> Register ( string userName, string password)
{ {
if (await UserExists(user.UserName)) if (await UserExists(userName))
{ {
return new ServiceResponse<int> { Success = false, Message = "User already exist." }; return new ServiceResponse<int> { Success = false, Message = "User already exist." };
} }
CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt); CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
var user = new UserAccount
user.PasswordHash = passwordHash; {
user.PasswordSalt = passwordSalt; UserName = userName,
PasswordHash = passwordHash,
PasswordSalt = passwordSalt
};
_context.UserAccounts.Add(user); _context.UserAccounts.Add(user);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();

View File

@@ -5,7 +5,7 @@ namespace BlazorPolicyAuth.Services.AuthService;
public interface IAuthService public interface IAuthService
{ {
Task<ServiceResponse<int>> Register(UserAccount user, string password); Task<ServiceResponse<int>> Register(string userName, string password);
Task<bool> UserExists(string email); Task<bool> UserExists(string email);
Task<ServiceResponse<string>> Login(string email, string password); Task<ServiceResponse<string>> Login(string email, string password);
Task<ServiceResponse<bool>> ChangePassword(int userId, string newPassword); Task<ServiceResponse<bool>> ChangePassword(int userId, string newPassword);