[frontend/pages] Add change password section to account settings

This commit is contained in:
pancakes 2025-03-24 16:58:22 +10:00
parent cd0149cfeb
commit 897fe6e55a
No known key found for this signature in database

View file

@ -12,6 +12,7 @@
@attribute [Authorize] @attribute [Authorize]
@layout SettingsLayout @layout SettingsLayout
@inject ApiService Api; @inject ApiService Api;
@inject GlobalComponentSvc Global;
@inject ILogger<Profile> Logger; @inject ILogger<Profile> Logger;
@inject IStringLocalizer<Localization> Loc; @inject IStringLocalizer<Localization> Loc;
@inject IJSRuntime Js; @inject IJSRuntime Js;
@ -73,6 +74,19 @@
</EditForm> </EditForm>
</div> </div>
<h1>@Loc["Change password"]</h1>
<EditForm OnSubmit="ChangePassword" Model="PasswordForm">
<InputText @bind-Value="PasswordForm.OldPassword" type="password" class="input" placeholder="@Loc["Old password"]" required/>
<InputText @bind-Value="PasswordForm.NewPassword" type="password" class="input" placeholder="@Loc["New password"]" minlength="8" required/>
<StateButton ExtraClasses="button" @ref="@PasswordButton" OnClick="ChangePassword">
<Initial>@Loc["Update password"]</Initial>
<Success>@Loc["Updated"]</Success>
<Loading>@Loc["Loading"]<LoadingSpinner />
</Loading>
<Failed>@Loc["Error"]</Failed>
</StateButton>
</EditForm>
<h1>@Loc["Two-Factor Authentication"]</h1> <h1>@Loc["Two-Factor Authentication"]</h1>
@if (SettingsForm.TwoFactorEnrolled == false) @if (SettingsForm.TwoFactorEnrolled == false)
{ {
@ -140,8 +154,10 @@
@code { @code {
private UserSettingsResponse SettingsForm { get; set; } = null!; private UserSettingsResponse SettingsForm { get; set; } = null!;
private ChangePasswordRequest PasswordForm { get; set; } = null!;
private State State { get; set; } = State.Loading; private State State { get; set; } = State.Loading;
private StateButton SaveButton { get; set; } = null!; private StateButton SaveButton { get; set; } = null!;
private StateButton PasswordButton { get; set; } = null!;
private StateButton DisableButton { get; set; } = null!; private StateButton DisableButton { get; set; } = null!;
private StateButton TwoFactorButton { get; set; } = null!; private StateButton TwoFactorButton { get; set; } = null!;
private ElementReference EnrollDialog { get; set; } private ElementReference EnrollDialog { get; set; }
@ -163,6 +179,7 @@
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
SettingsForm = await Api.Settings.GetSettingsAsync(); SettingsForm = await Api.Settings.GetSettingsAsync();
PasswordForm = new ChangePasswordRequest { OldPassword = "", NewPassword = "" };
State = State.Loaded; State = State.Loaded;
} }
@ -181,6 +198,29 @@
} }
} }
private async Task ChangePassword()
{
if (PasswordButton.State == StateButton.StateEnum.Loading || string.IsNullOrWhiteSpace(PasswordForm.OldPassword) || PasswordForm.NewPassword.Length < 8) return;
PasswordButton.State = StateButton.StateEnum.Loading;
try
{
var res = await Api.Auth.ChangePasswordAsync(PasswordForm);
PasswordButton.State = res.Status == AuthStatusEnum.Authenticated
? StateButton.StateEnum.Success
: StateButton.StateEnum.Failed;
PasswordForm.OldPassword = "";
PasswordForm.NewPassword = "";
}
catch (ApiException e)
{
await Global.NoticeDialog?.Display(e.Response.Message ?? @Loc["An unknown error occurred"], NoticeDialog.NoticeType.Error)!;
PasswordButton.State = StateButton.StateEnum.Failed;
}
}
private async Task Enroll2FA() private async Task Enroll2FA()
{ {
var module = await _moduleTask.Value; var module = await _moduleTask.Value;