@@ -50,6 +50,8 @@
private async Task Authenticate()
{
+ Console.WriteLine("***");
+ Console.WriteLine(userLogin.UserName);
var result = await AuthService.Login(userLogin.UserName, userLogin.Password);
if (result.Success)
{
diff --git a/BlazorPolicyAuth/Components/Pages/Account/Register.razor b/BlazorPolicyAuth/Components/Pages/Account/Register.razor
new file mode 100644
index 0000000..b32b85f
--- /dev/null
+++ b/BlazorPolicyAuth/Components/Pages/Account/Register.razor
@@ -0,0 +1,57 @@
+@page "/register"
+@using BlazorPolicyAuth.Models.ViewModels
+
+@inject AuthenticationStateProvider AuthenticationStateProvider
+@inject NavigationManager NavigationManager
+@inject IAuthService AuthService
+
+
Register
+
+
+
+
+
+
Register Form
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ @errorMessage
+
+
+@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";
+ }
+}
\ No newline at end of file
diff --git a/BlazorPolicyAuth/Models/ViewModels/UserLogin.cs b/BlazorPolicyAuth/Models/ViewModels/UserLogin.cs
index 6c20e0a..1490c6d 100644
--- a/BlazorPolicyAuth/Models/ViewModels/UserLogin.cs
+++ b/BlazorPolicyAuth/Models/ViewModels/UserLogin.cs
@@ -4,8 +4,9 @@ namespace BlazorPolicyAuth.Models.ViewModels;
public class UserLogin
{
- [Required, EmailAddress]
+ //[Required, EmailAddress]
+ //[Required]
public string UserName { get; set; } = string.Empty;
- [Required]
+ //[Required]
public string Password { get; set; }
}
\ No newline at end of file
diff --git a/BlazorPolicyAuth/Models/ViewModels/UserRegister.cs b/BlazorPolicyAuth/Models/ViewModels/UserRegister.cs
index ec3dd3f..af43a5e 100644
--- a/BlazorPolicyAuth/Models/ViewModels/UserRegister.cs
+++ b/BlazorPolicyAuth/Models/ViewModels/UserRegister.cs
@@ -4,9 +4,9 @@ namespace BlazorPolicyAuth.Models.ViewModels
{
public class UserRegister
{
- [Required, EmailAddress]
+ //[Required, EmailAddress]
public string Email { get; set; } = string.Empty;
- [Required, StringLength(100, MinimumLength = 5)]
+ //[Required, StringLength(100, MinimumLength = 5)]
public string Password { get; set; } = string.Empty;
[Compare("Password", ErrorMessage = "The password do not match.")]
public string ConfirmPassword { get; set; } = string.Empty;
diff --git a/BlazorPolicyAuth/Services/AuthService/AuthService.cs b/BlazorPolicyAuth/Services/AuthService/AuthService.cs
index e9a015e..349901f 100644
--- a/BlazorPolicyAuth/Services/AuthService/AuthService.cs
+++ b/BlazorPolicyAuth/Services/AuthService/AuthService.cs
@@ -25,17 +25,20 @@ public class AuthService : IAuthService
_httpContextAccessor = httpContextAccessor;
}
- public async Task
> Register(UserAccount user, string password)
+ public async Task> Register ( string userName, string password)
{
- if (await UserExists(user.UserName))
+ if (await UserExists(userName))
{
return new ServiceResponse { Success = false, Message = "User already exist." };
}
CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
-
- user.PasswordHash = passwordHash;
- user.PasswordSalt = passwordSalt;
+ var user = new UserAccount
+ {
+ UserName = userName,
+ PasswordHash = passwordHash,
+ PasswordSalt = passwordSalt
+ };
_context.UserAccounts.Add(user);
await _context.SaveChangesAsync();
diff --git a/BlazorPolicyAuth/Services/AuthService/IAuthService.cs b/BlazorPolicyAuth/Services/AuthService/IAuthService.cs
index ce36661..6ef0258 100644
--- a/BlazorPolicyAuth/Services/AuthService/IAuthService.cs
+++ b/BlazorPolicyAuth/Services/AuthService/IAuthService.cs
@@ -5,7 +5,7 @@ namespace BlazorPolicyAuth.Services.AuthService;
public interface IAuthService
{
- Task> Register(UserAccount user, string password);
+ Task> Register(string userName, string password);
Task UserExists(string email);
Task> Login(string email, string password);
Task> ChangePassword(int userId, string newPassword);