bulletboards/ImageBoardServerApp/Pages/Accounts/ClickOn/ClickOnResetPassword.razor

78 lines
2.1 KiB
Text
Raw Permalink Normal View History

@page "/sys/click/resetpw/{userid}/{hash}"
@using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Data.Repository
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager
<PageTitle>Password reset</PageTitle>
<span>Password reset for account id #@userid</span>
<div class="login">
<form>
<RadzenFormField Text="New Password" Variant="Variant.Outlined">
<RadzenPassword @bind-Value="@Password"/>
</RadzenFormField>
<br/>
<br/>
<RadzenButton Click=@reset Text="reset" ButtonStyle="ButtonStyle.Secondary"/>
<br/>
</form>
@if (msg != null)
{
<span>@msg</span>
}
</div>
@code {
private string Password { get; set; }
private string msg { get; set; }
[Parameter]
[Required]
public string userid { get; set; }
[Parameter]
[Required]
public string hash { get; set; }
public async void reset()
{
if (!int.TryParse(userid, out _))
{
msg = "malformed userid.";
return;
}
Console.WriteLine("Resetting a password...");
var user = await UsersRepository.getUserByIdAsync(int.Parse(userid));
if (user == null)
{
msg = "This user does not exist.";
return;
}
if (user.ResetPasswordToken != hash)
{
msg = "The token does not match the account.";
return;
}
if (user.ResetPasswordExpiresAt < 0 || user.ResetPasswordToken == "-1")
{
msg = "There is currently no valid link to reset this accounts password.";
return;
}
if (user.ResetPasswordExpiresAt < DateTimeOffset.Now.ToUnixTimeMilliseconds())
{
msg = "This link has expired.";
return;
}
user.Password = Password = BCrypt.Net.BCrypt.HashPassword(Password);
user.ResetPasswordToken = "-1";
user.ResetPasswordExpiresAt = -1;
await UsersRepository.updateUserAsync(user);
msg = "Your Password has been updated.";
}
}