mirror of
https://github.com/CannedNet/CannedNet.git
synced 2026-10-25 23:18:54 -07:00
107 lines
3.6 KiB
Plaintext
107 lines
3.6 KiB
Plaintext
@page "/change-password"
|
|
@attribute [Authorize]
|
|
@rendermode InteractiveServer
|
|
@using System.Security.Claims
|
|
@using CannedNet.Admin.Services
|
|
@inject AdminAuthService AuthService
|
|
@inject NavigationManager Navigation
|
|
@inject IJSRuntime JSRuntime
|
|
|
|
<PageTitle>Change Password</PageTitle>
|
|
|
|
<h3>Change Password</h3>
|
|
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-header">Change Your Password</div>
|
|
<div class="card-body">
|
|
<form>
|
|
<div class="mb-3">
|
|
<label class="form-label">Current Password</label>
|
|
<input type="password" class="form-control" @bind="currentPassword" required />
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">New Password</label>
|
|
<input type="password" class="form-control" @bind="newPassword" required minlength="6" />
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Confirm New Password</label>
|
|
<input type="password" class="form-control" @bind="confirmNewPassword" required />
|
|
</div>
|
|
<button type="submit" class="btn btn-primary" @onclick="DoChangePassword" disabled="@saving">
|
|
@(saving ? "Changing..." : "Change Password")
|
|
</button>
|
|
<a href="/" class="btn btn-outline-secondary ms-2">Cancel</a>
|
|
@if (!string.IsNullOrEmpty(message))
|
|
{
|
|
<div class="alert mt-3 @(message.Contains("Error") ? "alert-danger" : "alert-success")">@message</div>
|
|
}
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
@code {
|
|
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
|
|
|
|
private string currentPassword = "";
|
|
private string newPassword = "";
|
|
private string confirmNewPassword = "";
|
|
private bool saving;
|
|
private string? message;
|
|
|
|
private async Task DoChangePassword()
|
|
{
|
|
saving = true;
|
|
message = null;
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(currentPassword))
|
|
{
|
|
message = "Current password is required.";
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(newPassword) || newPassword.Length < 6)
|
|
{
|
|
message = "New password must be at least 6 characters.";
|
|
return;
|
|
}
|
|
if (newPassword != confirmNewPassword)
|
|
{
|
|
message = "New passwords do not match.";
|
|
return;
|
|
}
|
|
|
|
var state = await AuthState!;
|
|
var userIdClaim = state.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
if (userIdClaim == null || !int.TryParse(userIdClaim, out var userId))
|
|
{
|
|
message = "Error: Could not identify user.";
|
|
return;
|
|
}
|
|
|
|
if (!AuthService.ChangePassword(userId, currentPassword, newPassword))
|
|
{
|
|
message = "Error: Current password is incorrect.";
|
|
return;
|
|
}
|
|
|
|
message = "Password changed successfully!";
|
|
currentPassword = "";
|
|
newPassword = "";
|
|
confirmNewPassword = "";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
message = $"Error: {ex.Message}";
|
|
}
|
|
finally
|
|
{
|
|
saving = false;
|
|
}
|
|
}
|
|
}
|