|
| 1 | +@using CodeBeam.UltimateAuth.Core.Contracts |
| 2 | +@using CodeBeam.UltimateAuth.Credentials.Contracts |
| 3 | +@using CodeBeam.UltimateAuth.Users.Contracts |
| 4 | +@inject IUAuthClient UAuthClient |
| 5 | +@inject ISnackbar Snackbar |
| 6 | +@inject IDialogService DialogService |
| 7 | +@inject IUAuthStateManager StateManager |
| 8 | +@inject NavigationManager Nav |
| 9 | + |
| 10 | +<MudDialog Class="mud-width-full" ContentClass="uauth-dialog"> |
| 11 | + <TitleContent> |
| 12 | + <MudText>Credential Management</MudText> |
| 13 | + <MudText Typo="Typo.subtitle2" Color="Color.Primary">User: @AuthState?.Identity?.DisplayName</MudText> |
| 14 | + </TitleContent> |
| 15 | + <DialogContent> |
| 16 | + <MudForm @ref="@_form" OnEnterPressed="@ChangePasswordAsync"> |
| 17 | + <MudGrid Spacing="2"> |
| 18 | + @if (UserKey == null) |
| 19 | + { |
| 20 | + <MudItem xs="12" sm="6"> |
| 21 | + <MudPasswordField @bind-Value="_oldPassword" @bind-PasswordMode="@_passwordMode1" Label="Old Password" Variant="Variant.Outlined" Immediate="true" Required="true" /> |
| 22 | + </MudItem> |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + <MudItem xs="12"> |
| 27 | + <MudAlert Severity="Severity.Warning"> |
| 28 | + Administrators can directly assign passwords to users. |
| 29 | + However, using the credential reset flow is generally recommended for better security and auditability. |
| 30 | + </MudAlert> |
| 31 | + </MudItem> |
| 32 | + } |
| 33 | + |
| 34 | + <MudItem xs="12" sm="6"> |
| 35 | + <MudPasswordField @bind-Value="_newPassword" @bind-PasswordMode="@_passwordMode2" Label="New Password" Variant="Variant.Outlined" Immediate="true" Required="true" /> |
| 36 | + </MudItem> |
| 37 | + |
| 38 | + <MudItem xs="12" sm="6"> |
| 39 | + <MudPasswordField @bind-Value="_newPasswordCheck" @bind-PasswordMode="@_passwordMode3" Label="New Password (Again)" Variant="Variant.Outlined" Immediate="true" Required="true" Validation="@(new Func<string, string>(PasswordMatch))" /> |
| 40 | + </MudItem> |
| 41 | + |
| 42 | + <MudItem xs="12"> |
| 43 | + <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="ChangePasswordAsync">@(UserKey is null ? "Change Password" : "Set Password")</MudButton> |
| 44 | + </MudItem> |
| 45 | + </MudGrid> |
| 46 | + </MudForm> |
| 47 | + </DialogContent> |
| 48 | + <DialogActions> |
| 49 | + <MudButton OnClick="Cancel">Cancel</MudButton> |
| 50 | + </DialogActions> |
| 51 | +</MudDialog> |
| 52 | + |
| 53 | +@code { |
| 54 | + private MudForm _form = null!; |
| 55 | + private string? _oldPassword; |
| 56 | + private string? _newPassword; |
| 57 | + private string? _newPasswordCheck; |
| 58 | + private bool _passwordMode1 = false; |
| 59 | + private bool _passwordMode2 = false; |
| 60 | + private bool _passwordMode3 = true; |
| 61 | + |
| 62 | + [CascadingParameter] |
| 63 | + private IMudDialogInstance MudDialog { get; set; } = default!; |
| 64 | + |
| 65 | + [Parameter] |
| 66 | + public UAuthState AuthState { get; set; } = default!; |
| 67 | + |
| 68 | + [Parameter] |
| 69 | + public UserKey? UserKey { get; set; } |
| 70 | + |
| 71 | + private async Task ChangePasswordAsync() |
| 72 | + { |
| 73 | + if (_form is null) |
| 74 | + return; |
| 75 | + |
| 76 | + await _form.Validate(); |
| 77 | + if (!_form.IsValid) |
| 78 | + { |
| 79 | + Snackbar.Add("Form is not valid.", Severity.Error); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (_newPassword != _newPasswordCheck) |
| 84 | + { |
| 85 | + Snackbar.Add("New password and check do not match", Severity.Error); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + ChangeCredentialRequest request; |
| 90 | + |
| 91 | + if (UserKey is null) |
| 92 | + { |
| 93 | + request = new ChangeCredentialRequest |
| 94 | + { |
| 95 | + CurrentSecret = _oldPassword!, |
| 96 | + NewSecret = _newPassword! |
| 97 | + }; |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + request = new ChangeCredentialRequest |
| 102 | + { |
| 103 | + NewSecret = _newPassword! |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + UAuthResult<ChangeCredentialResult> result; |
| 108 | + if (UserKey is null) |
| 109 | + { |
| 110 | + result = await UAuthClient.Credentials.ChangeMyAsync(request); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + result = await UAuthClient.Credentials.ChangeCredentialAsync(UserKey.Value, request); |
| 115 | + } |
| 116 | + |
| 117 | + if (result.IsSuccess) |
| 118 | + { |
| 119 | + Snackbar.Add("Password changed successfully", Severity.Success); |
| 120 | + _oldPassword = null; |
| 121 | + _newPassword = null; |
| 122 | + _newPasswordCheck = null; |
| 123 | + MudDialog.Close(DialogResult.Ok(true)); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + Snackbar.Add(result.GetErrorText ?? "An error occurred while changing password", Severity.Error); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private string PasswordMatch(string arg) => _newPassword != arg ? "Passwords don't match" : string.Empty; |
| 132 | + |
| 133 | + private void Cancel() => MudDialog.Cancel(); |
| 134 | +} |
0 commit comments