Add register page, associated view model and successfully call service that just sends back received data
This commit is contained in:
@@ -14,6 +14,11 @@
|
||||
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
|
||||
</NavLink>
|
||||
</div>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="register">
|
||||
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Register
|
||||
</NavLink>
|
||||
</div>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="counter">
|
||||
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Counter
|
||||
|
||||
8
Models/ViewModels/ServiceResponse.cs
Normal file
8
Models/ViewModels/ServiceResponse.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Sandbox.Models.ViewModels;
|
||||
|
||||
public class ServiceResponse<T>
|
||||
{
|
||||
public T? Data { get; set; }
|
||||
public bool Success { get; set; } = true;
|
||||
public string Message { get; set; } = string.Empty;
|
||||
}
|
||||
10
Models/ViewModels/UserRegister.cs
Normal file
10
Models/ViewModels/UserRegister.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Sandbox.Models.ViewModels;
|
||||
|
||||
public class UserRegister
|
||||
{
|
||||
public string Email { get; set; } = string.Empty;
|
||||
//[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;
|
||||
}
|
||||
62
Pages/Account/Register.razor
Normal file
62
Pages/Account/Register.razor
Normal file
@@ -0,0 +1,62 @@
|
||||
@page "/register"
|
||||
@using Sandbox.Services.AuthService
|
||||
@using Sandbox.Models.ViewModels
|
||||
|
||||
@* @inject AuthenticationStateProvider AuthenticationStateProvider *@
|
||||
@* @inject NavigationManager NavigationManager *@
|
||||
@inject IAuthService AuthService
|
||||
|
||||
<title>Register</title>
|
||||
|
||||
<EditForm Model="user" OnValidSubmit="HandleRegistration">
|
||||
<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>
|
||||
|
||||
<div class="@messageCssClass">
|
||||
<span>@message</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";
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using blazor_mini_sandbox;
|
||||
using Sandbox;
|
||||
using Sandbox.Services.AuthService;
|
||||
|
||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
builder.RootComponents.Add<App>("#app");
|
||||
@@ -8,4 +9,6 @@ builder.RootComponents.Add<HeadOutlet>("head::after");
|
||||
|
||||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
|
||||
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
|
||||
16
Services/AuthService/AuthService.cs
Normal file
16
Services/AuthService/AuthService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Sandbox.Models.ViewModels;
|
||||
|
||||
namespace Sandbox.Services.AuthService;
|
||||
|
||||
public class AuthService : IAuthService
|
||||
{
|
||||
public async Task<ServiceResponse<int>> Register(string userName, string password)
|
||||
{
|
||||
return new ServiceResponse<int>
|
||||
{
|
||||
Data = 1,
|
||||
Message = $"Got register request with user name {userName}",
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
8
Services/AuthService/IAuthService.cs
Normal file
8
Services/AuthService/IAuthService.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Sandbox.Models.ViewModels;
|
||||
|
||||
namespace Sandbox.Services.AuthService;
|
||||
|
||||
public interface IAuthService
|
||||
{
|
||||
Task<ServiceResponse<int>> Register(string userName, string password);
|
||||
}
|
||||
@@ -6,5 +6,5 @@
|
||||
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
||||
@using Microsoft.AspNetCore.Components.WebAssembly.Http
|
||||
@using Microsoft.JSInterop
|
||||
@using blazor_mini_sandbox
|
||||
@using blazor_mini_sandbox.Layout
|
||||
@using Sandbox
|
||||
@using Sandbox.Layout
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<OverrideHtmlAssetPlaceholders>true</OverrideHtmlAssetPlaceholders>
|
||||
<RootNamespace>Sandbox</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user