Files
Nemera/src/Lumina.Presentation.Web/Views/Auth/ChangePassword.cshtml
T

137 lines
6.5 KiB
Plaintext

@*
/auth/change-password/
*@
@using Lumina.Presentation.Web.Common.Models.UsersManagement
@using Lumina.Presentation.Web.Common.Services
@using Microsoft.AspNetCore.Mvc.Localization
@{
// generate a unique id for the scripts section of this view, so it can be dynamically loaded/unloaded by the global AJAX navigator handler
var scriptId = ScriptIdentifierHelper.GenerateScriptId();
}
@inject IUrlService _urlService
@inject IViewLocalizer _localizer
@model ChangePasswordRequestModel
<div style="display: grid; grid-template-rows: auto 1fr;"><!--container with two rows, one fixed, one filling remaining space-->
<div style="min-height: 1rem;"></div><!--the first row is a dummy row for "margin", because setting a margin on the next row would offset the parent's position-->
<div class="enlightenment-panel shadow-effect p-2 mx-auto panel w-55 mb-10" style="min-width: 320px; margin-top: 1rem;">
<div class="shine-effect"></div>
<form method="post" id="change-password-form" class="inline">
@Html.AntiForgeryToken()
<input type="text" name="username" autocomplete="username" value="@User.Identity!.Name" hidden />
<div class="form-container">
<!-- Current Password -->
<div class="form-row" id="div-current-password">
<div class="form-label">
<label asp-for="CurrentPassword">@_localizer["CurrentPassword"]</label>
<span class="mandatory-field">*</span>:
</div>
<div class="form-field">
<input type="password" asp-for="CurrentPassword" autocomplete="new-password" class="enlightenment-input" />
</div>
<small class="form-label text-disabled form-description">@_localizer["DescriptionCurrentPassword"]</small>
</div>
<!-- New Password -->
<div class="form-row" id="div-new-password">
<div class="form-label">
<label asp-for="NewPassword">@_localizer["NewPassword"]</label>
<span class="mandatory-field">*</span>:
</div>
<div class="form-field">
<input type="password" asp-for="NewPassword" autocomplete="new-password" class="enlightenment-input" />
</div>
<small class="form-label text-disabled form-description">@_localizer["DescriptionNewPassword"]</small>
</div>
<!-- New Password Confirm-->
<div class="form-row" id="div-new-password-confirm">
<div class="form-label">
<label asp-for="NewPasswordConfirm">@_localizer["NewPasswordConfirm"]</label>
<span class="mandatory-field">*</span>:
</div>
<div class="form-field">
<input type="password" asp-for="NewPasswordConfirm" autocomplete="new-password" class="enlightenment-input" />
</div>
<small class="form-label text-disabled form-description">@_localizer["DescriptionNewPasswordConfirm"]</small>
</div>
<div class="form-row" id="div-change-password-submit" style="justify-content: center;">
<button type="submit" class="confirm-button h-22px">@_localizer["ChangePasswordKey"]</button>
</div>
</div>
</form>
<!-- section registration success -->
<div class="text-center w-100">
<p id="change-password-success" class="text-light-one w-100 hidden">@_localizer["PasswordChanged"]</p>
</div>
</div>
</div>
@section Scripts {
<script type="text/javascript" data-form="@scriptId" defer>
// create cleanup registry for this view
const cleanupFunctions = [];
const form = document.getElementById('change-password-form');
const txtCurrentPassword = form.querySelector('[name="CurrentPassword"]');
const txtNewPassword = form.querySelector('[name="NewPassword"]');
const txtNewPasswordConfirm = form.querySelector('[name="NewPasswordConfirm"]');
const changePasswordSuccess = document.getElementById('change-password-success');
const divCurrentPassword = document.getElementById('div-current-password');
const divNewPassword = document.getElementById('div-new-password');
const divNewPasswordConfirm = document.getElementById('div-new-password-confirm');
const divChangePasswordSubmit = document.getElementById('div-change-password-submit');
// perform the password change
async function handleChangePasswordAsync() {
const formData = {
CurrentPassword: txtCurrentPassword.value,
NewPassword: txtNewPassword.value,
NewPasswordConfirm: txtNewPasswordConfirm.value
};
const data = await callApiPostAsync('@_urlService.GetAbsoluteUrl("ChangePasswordPost", "Auth")', formData);
if (data && data.isPasswordChanged === true) {
changePasswordSuccess.classList.replace('hidden', 'block');
divCurrentPassword.classList.add('hidden');
divNewPassword.classList.add('hidden');
divNewPasswordConfirm.classList.add('hidden');
divChangePasswordSubmit.classList.add('hidden');
}
}
// on Enter key pressed
const handleEnterKey = async function(e) {
if (e.key === 'Enter') {
e.preventDefault();
await handleChangePasswordAsync();
}
};
// on form submit event
const handleSubmit = async function(e) {
e.preventDefault();
await handleChangePasswordAsync();
};
// add listeners
form.addEventListener('keypress', handleEnterKey);
form.addEventListener('submit', handleSubmit);
// register cleanup for these listeners
cleanupFunctions.push(() => {
form.removeEventListener('keypress', handleEnterKey);
form.removeEventListener('submit', handleSubmit);
});
// store all cleanups for this view on window object using the scriptId
window.viewCleanups = window.viewCleanups || {};
window.viewCleanups['@scriptId'] = () => {
cleanupFunctions.forEach(cleanup => cleanup());
console.log(`Unloaded @scriptId`);
};
console.log(`Loaded @scriptId`);
</script>
}