Files
SecureBank/SecureBank/Components/Pages/User/ChangePassword.razor
2024-01-23 15:41:59 +01:00

141 lines
3.8 KiB
Plaintext

@page "/account-details/change-password"
<h3>Change password</h3>
@if (_authLoaded)
{
@if (_claims is not null)
{
<form>
<div class="form-group">
<label for="password1-input">
Password:
</label>
<InputText id="password1-input" class="form-control" type="password" @bind-Value="_password1" data-index="1" minlength="8" maxlength="20"></InputText>
</div>
<div class="form-group">
<label for="password2-input">
Confirm password:
</label>
<InputText id="password2-input" class="form-control" type="password" @bind-Value="_password2" data-index="1" minlength="8" maxlength="20"></InputText>
</div>
</form>
<br />
<button type="submit" class="btn btn-primary" @onclick="SubmitChangePassword">Submit</button>
@if (!string.IsNullOrWhiteSpace(_messageError))
{
<p class="text-red">Error: @_messageError</p>
}
@if (!string.IsNullOrWhiteSpace(_messageSuccess))
{
<p class="text-green">@_messageSuccess</p>
}
}
else
{
<p>You do not have permission to view this page</p>
<a href="/">Click here to redirect to main page</a>
}
}
else
{
<p>Waiting for authorization...</p>
}
@code {
#region SERVICES
[Inject]
protected IAccountsService _accountsService { get; set; }
[Inject]
protected AuthenticationHelper _authenticationHelper { get; set; }
[Inject]
protected NavigationManager _navigationManager { get; set; }
[Inject]
protected TokenAuthenticationStateProvider _authenticationStateProvider { get; set; }
[Inject]
protected HttpClient _httpClient { get; set; }
#endregion
#region FIELDS
protected Claims? _claims;
protected bool _authLoaded;
protected string? _messageError;
protected string? _messageSuccess;
protected string _password1;
protected string _password2;
#endregion
#region METHODS
protected override void OnInitialized()
{
_claims = null;
_authLoaded = false;
_messageError = null;
_messageSuccess = null;
_password1 = string.Empty;
_password2 = string.Empty;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
AuthenticationState state = await _authenticationStateProvider.GetAuthenticationStateAsync();
if (state.User.Claims.Any())
{
_claims = new Claims(state.User.Claims);
}
_authLoaded = true;
StateHasChanged();
}
}
protected async Task SubmitChangePassword()
{
if (string.Equals(_password1, _password2))
{
ChangePasswordRequest data = new ChangePasswordRequest { Password = _password1 };
APIResponse response = await _accountsService.ChangePassword(data);
if (response.Status == ResponseStatus.Ok)
{
_messageError = null;
_messageSuccess = "Password has been changed. You will be logged out in 5 seconds.";
StateHasChanged();
Task redirectionTimer = Task.Delay(5000);
await _authenticationHelper.RemoveToken();
_httpClient.DefaultRequestHeaders.Authorization = null;
await redirectionTimer;
_navigationManager.NavigateTo("/", true);
}
else
{
_messageError = response.Message;
}
}
else
{
_messageError = "Password fields does not match";
}
}
#endregion
}