LANCommander/LANCommander.Server/UI/Pages/Profile/ChangePassword.razor

76 lines
2.4 KiB
Text

@page "/Profile/ChangePassword"
@using Microsoft.AspNetCore.Components.Authorization;
@layout ProfileLayout
@inject DatabaseServiceFactory DatabaseServiceFactory
@inject IMessageService MessageService
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject ILogger<ChangePassword> Logger
@attribute [Authorize]
<PageHeader Title="Change Password" />
<div style="padding: 0 24px;">
<Form Model="Model" Layout="@FormLayout.Vertical">
<FormItem Label="Current Password">
<InputPassword @bind-Value="context.CurrentPassword" />
</FormItem>
<FormItem Label="New Password">
<InputPassword @bind-Value="context.NewPassword" />
</FormItem>
<FormItem Label="Confirm Password">
<InputPassword @bind-Value="context.NewPasswordConfirm" />
</FormItem>
<FormItem>
<Button OnClick="Change" Type="@ButtonType.Primary">Change</Button>
</FormItem>
</Form>
</div>
@code {
User User = new User();
ChangePasswordModel Model = new ChangePasswordModel();
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
using (var userService = DatabaseServiceFactory.Create<UserService>())
{
if (authState.User.Identity.IsAuthenticated)
User = await userService.GetAsync(authState.User.Identity.Name);
}
}
async void Change()
{
try
{
if (Model.NewPassword == Model.NewPasswordConfirm)
{
using (var userService = DatabaseServiceFactory.Create<UserService>())
{
var result = await userService.ChangePassword(User.UserName, Model.CurrentPassword, Model.NewPassword);
if (result.Succeeded)
MessageService.Success("Password changed!");
}
}
else
await MessageService.Error("Passwords don't match!");
}
catch (Exception ex)
{
await MessageService.Error("Password could not be changed!");
Logger.LogError(ex, "Password could not be changed!");
}
}
public class ChangePasswordModel
{
public string CurrentPassword { get; set; }
public string NewPassword { get; set; }
public string NewPasswordConfirm { get; set; }
}
}