Files
SharepointToolbox-Web/Components/Pages/Account/ChangePassword.razor
T

93 lines
2.9 KiB
Plaintext

@page "/account/change-password"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@inject IUserService UserService
@inject IUserContextAccessor UserContext
@inject IAuditService Audit
@inject TranslationSource T
@rendermode InteractiveServer
@using SharepointToolbox.Web.Core.Models
@using SharepointToolbox.Web.Services.Audit
@using SharepointToolbox.Web.Services.Auth
@using SharepointToolbox.Web.Services.Session
<h1 class="page-title">@T["changepw.title"]</h1>
@if (!UserContext.IsAuthenticated)
{
<div class="alert alert-error">@T["changepw.mustsignin"]</div>
return;
}
@if (_user is null)
{
<p class="page-subtitle">@T["changepw.loading"]</p>
}
else if (_user.Provider != AuthProvider.Local)
{
<div class="alert alert-info">
@T["changepw.entra"]
</div>
}
else
{
@if (!string.IsNullOrEmpty(_message))
{
<div class="alert @(_isError ? "alert-error" : "alert-success")">@_message</div>
}
<div class="card" style="max-width:420px">
<label class="form-label" for="cur">@T["changepw.current"]</label>
<input id="cur" class="form-input" type="password" @bind="_current" autocomplete="current-password" />
<label class="form-label" for="new" style="margin-top:12px">@T["changepw.new"]</label>
<input id="new" class="form-input" type="password" @bind="_new" autocomplete="new-password" />
<label class="form-label" for="confirm" style="margin-top:12px">@T["changepw.confirm"]</label>
<input id="confirm" class="form-input" type="password" @bind="_confirm" autocomplete="new-password" />
<div style="margin-top:14px">
<button class="btn btn-primary" @onclick="SubmitAsync">@T["changepw.submit"]</button>
</div>
</div>
}
@code {
private AppUser? _user;
private string _current = string.Empty;
private string _new = string.Empty;
private string _confirm = string.Empty;
private string _message = string.Empty;
private bool _isError;
protected override async Task OnInitializedAsync()
{
if (UserContext.IsAuthenticated)
_user = await UserService.GetByEmailAsync(UserContext.Email);
}
private async Task SubmitAsync()
{
if (_user is null) return;
if (string.IsNullOrWhiteSpace(_new) || _new != _confirm)
{
_message = T["changepw.err.mismatch"];
_isError = true;
return;
}
var ok = await UserService.ChangePasswordAsync(_user.Id, _current, _new);
if (ok)
{
await Audit.LogAsync("PasswordChanged", "", Array.Empty<string>(),
$"Changed own password ({_user.Email}).");
_message = T["changepw.success"];
_isError = false;
_current = _new = _confirm = string.Empty;
}
else
{
_message = T["changepw.err.incorrect"];
_isError = true;
}
}
}