84 lines
3.0 KiB
Plaintext
84 lines
3.0 KiB
Plaintext
@page "/register"
|
|
@layout AuthLayout
|
|
@using Microsoft.AspNetCore.Identity
|
|
@inject UserManager<ApplicationUser> UserManager
|
|
@inject SignInManager<ApplicationUser> SignInManager
|
|
@inject NavigationManager Navigation
|
|
|
|
<PageTitle>Budgeteer Beta — Register</PageTitle>
|
|
|
|
<h3 class="text-center mb-4">Create Account</h3>
|
|
|
|
<EditForm Model="Model" OnValidSubmit="HandleRegister" FormName="register">
|
|
<DataAnnotationsValidator />
|
|
|
|
@if (!string.IsNullOrEmpty(errorMessage))
|
|
{
|
|
<div class="alert alert-danger">@errorMessage</div>
|
|
}
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Email</label>
|
|
<InputText @bind-Value="Model.Email" class="form-control" placeholder="you@example.com" />
|
|
<ValidationMessage For="() => Model.Email" />
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Password</label>
|
|
<InputText @bind-Value="Model.Password" type="password" class="form-control" placeholder="At least 8 characters" />
|
|
<ValidationMessage For="() => Model.Password" />
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Confirm Password</label>
|
|
<InputText @bind-Value="Model.ConfirmPassword" type="password" class="form-control" />
|
|
<ValidationMessage For="() => Model.ConfirmPassword" />
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary w-100">Register</button>
|
|
</EditForm>
|
|
|
|
<p class="text-center mt-3">
|
|
Already have an account? <a href="/login">Sign in</a>
|
|
</p>
|
|
|
|
@code {
|
|
[SupplyParameterFromForm]
|
|
private RegisterModel Model { get; set; } = new();
|
|
|
|
private string? errorMessage;
|
|
|
|
private async Task HandleRegister()
|
|
{
|
|
errorMessage = null;
|
|
|
|
var user = new ApplicationUser { UserName = Model.Email, Email = Model.Email };
|
|
var result = await UserManager.CreateAsync(user, Model.Password);
|
|
|
|
if (result.Succeeded)
|
|
{
|
|
await SignInManager.SignInAsync(user, isPersistent: false);
|
|
Navigation.NavigateTo("/", forceLoad: true);
|
|
}
|
|
else
|
|
{
|
|
errorMessage = string.Join(" ", result.Errors.Select(e => e.Description));
|
|
}
|
|
}
|
|
|
|
private sealed class RegisterModel
|
|
{
|
|
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Email is required")]
|
|
[System.ComponentModel.DataAnnotations.EmailAddress(ErrorMessage = "Invalid email address")]
|
|
public string Email { get; set; } = string.Empty;
|
|
|
|
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Password is required")]
|
|
[System.ComponentModel.DataAnnotations.MinLength(8, ErrorMessage = "Password must be at least 8 characters")]
|
|
public string Password { get; set; } = string.Empty;
|
|
|
|
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Please confirm your password")]
|
|
[System.ComponentModel.DataAnnotations.Compare(nameof(Password), ErrorMessage = "Passwords do not match")]
|
|
public string ConfirmPassword { get; set; } = string.Empty;
|
|
}
|
|
}
|