2023-09-18 00:58:40 -05:00
|
|
|
@page "/Profile/ChangePassword"
|
|
|
|
|
@using Microsoft.AspNetCore.Components.Authorization;
|
2025-02-01 02:21:34 -06:00
|
|
|
@inject UserService UserService
|
2023-09-18 00:58:40 -05:00
|
|
|
@inject IMessageService MessageService
|
|
|
|
|
@inject AuthenticationStateProvider AuthenticationStateProvider
|
2024-07-07 16:33:46 -05:00
|
|
|
@inject ILogger<ChangePassword> Logger
|
2023-10-16 20:17:59 -05:00
|
|
|
@attribute [Authorize]
|
2023-09-18 00:58:40 -05:00
|
|
|
|
|
|
|
|
<PageHeader Title="Change Password" />
|
|
|
|
|
|
|
|
|
|
<div style="padding: 0 24px;">
|
2024-12-31 18:21:41 -06:00
|
|
|
<Form Model="Model" Layout="FormLayout.Vertical">
|
2025-01-06 02:18:28 -06:00
|
|
|
@if (!String.IsNullOrWhiteSpace(User.PasswordHash))
|
|
|
|
|
{
|
|
|
|
|
<FormItem Label="Current Password">
|
|
|
|
|
<InputPassword @bind-Value="context.CurrentPassword"/>
|
|
|
|
|
</FormItem>
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 00:58:40 -05:00
|
|
|
<FormItem Label="New Password">
|
2025-01-06 02:18:28 -06:00
|
|
|
<InputPassword @bind-Value="context.NewPassword"/>
|
2023-09-18 00:58:40 -05:00
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
|
|
<FormItem Label="Confirm Password">
|
2025-01-06 02:18:28 -06:00
|
|
|
<InputPassword @bind-Value="context.NewPasswordConfirm"/>
|
2023-09-18 00:58:40 -05:00
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
|
|
<FormItem>
|
|
|
|
|
<Button OnClick="Change" Type="@ButtonType.Primary">Change</Button>
|
|
|
|
|
</FormItem>
|
|
|
|
|
</Form>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
@code {
|
2024-10-23 02:31:50 -05:00
|
|
|
User User = new User();
|
2023-09-18 00:58:40 -05:00
|
|
|
ChangePasswordModel Model = new ChangePasswordModel();
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
2024-10-21 20:14:55 -05:00
|
|
|
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
2025-02-01 02:21:34 -06:00
|
|
|
|
|
|
|
|
if (authState.User.Identity.IsAuthenticated)
|
|
|
|
|
User = await UserService.GetAsync(authState.User.Identity.Name);
|
2023-09-18 00:58:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void Change()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
if (!String.IsNullOrWhiteSpace(User.PasswordHash) && !(await UserService.CheckPassword(User.UserName, Model.CurrentPassword)))
|
2025-01-06 02:18:28 -06:00
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
MessageService.Error("Incorrect password!");
|
|
|
|
|
return;
|
2025-01-06 02:18:28 -06:00
|
|
|
}
|
|
|
|
|
|
2023-09-18 00:58:40 -05:00
|
|
|
if (Model.NewPassword == Model.NewPasswordConfirm)
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
var result = await UserService.ChangePassword(User.UserName, Model.CurrentPassword, Model.NewPassword);
|
2023-09-18 00:58:40 -05:00
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
if (result.Succeeded)
|
|
|
|
|
MessageService.Success("Password changed!");
|
2023-09-18 00:58:40 -05:00
|
|
|
}
|
|
|
|
|
else
|
2025-01-06 02:18:28 -06:00
|
|
|
MessageService.Error("Passwords don't match!");
|
2023-09-18 00:58:40 -05:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-05-23 02:17:45 -05:00
|
|
|
await MessageService.ErrorAsync("Password could not be changed!");
|
2024-07-07 16:33:46 -05:00
|
|
|
Logger.LogError(ex, "Password could not be changed!");
|
2023-09-18 00:58:40 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ChangePasswordModel
|
|
|
|
|
{
|
|
|
|
|
public string CurrentPassword { get; set; }
|
|
|
|
|
public string NewPassword { get; set; }
|
|
|
|
|
public string NewPasswordConfirm { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|