Files
Aaru.Server/Aaru.Server.New/Components/Account/Pages/ResetPassword.razor

106 lines
3.9 KiB
Plaintext

@page "/Account/ResetPassword"
@using System.ComponentModel.DataAnnotations
@using System.Text
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using Aaru.Server.New.Data
@inject IdentityRedirectManager RedirectManager
@inject UserManager<ApplicationUser> UserManager
<PageTitle>Reset password</PageTitle>
<h1>Reset password</h1>
<h2>Reset your password.</h2>
<hr/>
<div class="row">
<div class="col-md-4">
<StatusMessage Message="@Message"/>
<EditForm FormName="reset-password" method="post" Model="Input" OnValidSubmit="OnValidSubmitAsync">
<DataAnnotationsValidator/>
<ValidationSummary class="text-danger" role="alert"/>
<input name="Input.Code" type="hidden" value="@Input.Code"/>
<div class="form-floating mb-3">
<InputText aria-required="true" autocomplete="username" @bind-Value="Input.Email" class="form-control" placeholder="name@example.com"/>
<label class="form-label" for="email">Email</label>
<ValidationMessage class="text-danger" For="() => Input.Email"/>
</div>
<div class="form-floating mb-3">
<InputText aria-required="true" autocomplete="new-password" @bind-Value="Input.Password" class="form-control" placeholder="Please enter your password." type="password"/>
<label class="form-label" for="password">Password</label>
<ValidationMessage class="text-danger" For="() => Input.Password"/>
</div>
<div class="form-floating mb-3">
<InputText aria-required="true" autocomplete="new-password" @bind-Value="Input.ConfirmPassword" class="form-control" placeholder="Please confirm your password." type="password"/>
<label class="form-label" for="confirm-password">Confirm password</label>
<ValidationMessage class="text-danger" For="() => Input.ConfirmPassword"/>
</div>
<button class="btn btn-lg btn-primary w-100" type="submit">Reset</button>
</EditForm>
</div>
</div>
@code {
private IEnumerable<IdentityError>? identityErrors;
[SupplyParameterFromForm]
private InputModel Input { get; set; } = new();
[SupplyParameterFromQuery]
private string? Code { get; set; }
private string? Message => identityErrors is null ? null : $"Error: {string.Join(", ", identityErrors.Select(error => error.Description))}";
protected override void OnInitialized()
{
if(Code is null)
{
RedirectManager.RedirectTo("Account/InvalidPasswordReset");
}
Input.Code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(Code));
}
private async Task OnValidSubmitAsync()
{
var user = await UserManager.FindByEmailAsync(Input.Email);
if(user is null)
{
// Don't reveal that the user does not exist
RedirectManager.RedirectTo("Account/ResetPasswordConfirmation");
}
var result = await UserManager.ResetPasswordAsync(user, Input.Code, Input.Password);
if(result.Succeeded)
{
RedirectManager.RedirectTo("Account/ResetPasswordConfirmation");
}
identityErrors = result.Errors;
}
private sealed class InputModel
{
[Required]
[EmailAddress]
public string Email { get; set; } = "";
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; } = "";
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; } = "";
[Required]
public string Code { get; set; } = "";
}
}