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

142 lines
6.2 KiB
Plaintext

@*
/auth/login/
*@
@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 LoginRequestModel
<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-50 mb-10" style="min-width: 320px;">
<div class="shine-effect"></div>
<form method="post" id="login-form" class="inline">
@Html.AntiForgeryToken()
<input type="hidden" name="ReturnUrl" value="@ViewData["ReturnUrl"]" />
<div class="form-container">
<!-- Username -->
<div class="form-row">
<div class="form-label">
<label asp-for="Username">@_localizer["Username"]</label>:
</div>
<div class="form-field">
<input asp-for="Username" autocomplete="username" class="enlightenment-input" />
</div>
</div>
<!-- Password -->
<div class="form-row">
<div class="form-label">
<label asp-for="Password">@_localizer["Password"]</label>:
</div>
<div class="form-field">
<input type="password" asp-for="Password" autocomplete="current-password" class="enlightenment-input" />
</div>
</div>
<div class="form-row">
<button id="btn-recover-password" type="button" class="abort-button h-22px">@_localizer["RecoverPassword"]</button>
<button id="btn-login" type="submit" class="confirm-button h-22px">@_localizer["LoginKey"]</button>
</div>
<!-- 2FA -->
<div class="form-row mt-10 hidden" id="div-totp-code">
<div class="form-label">
<label asp-for="TotpCode">@_localizer["TotpCode"]</label>
</div>
<div class="form-field">
<input type="number" asp-for="TotpCode" autocomplete="one-time-code" class="enlightenment-input" />
</div>
</div>
<div class="form-row">
<button type="submit" id="btn-validate-totp" class="confirm-button h-22px hidden">@_localizer["ValidateCode"]</button>
</div>
</div>
</form>
</div>
</div>
@section Scripts {
<script type="text/javascript" data-form="@scriptId" defer>
// create cleanup registry for this view
const cleanupFunctions = [];
const form = document.getElementById('login-form');
const returnUrlInput = form.querySelector('[name="ReturnUrl"]');
const txtUsername = form.querySelector('[name="Username"]');
const txtPassword = form.querySelector('[name="Password"]');
const txtTotpCode = form.querySelector('[name="TotpCode"]');
const divTotpCode = document.getElementById('div-totp-code');
const btnLogin = document.getElementById('btn-login');
const btnRecoverPassword = document.getElementById('btn-recover-password');
const btnValidateTotp = document.getElementById('btn-validate-totp');
// perform the authentication
async function handleLoginAsync() {
const formData = {
Username: txtUsername.value,
Password: txtPassword.value,
TotpCode: txtTotpCode.value,
ReturnUrl: returnUrlInput.value
};
const data = await callApiPostAsync('@_urlService.GetAbsoluteUrl("Login", "Auth")', formData);
if (data && data.isTotpRequired === true) {
txtUsername.disabled = true;
txtPassword.disabled = true;
btnLogin.disabled = true;
btnRecoverPassword.disabled = true;
divTotpCode.classList.replace('hidden', 'inline');
btnValidateTotp.classList.replace('hidden', 'block');
txtTotpCode.focus();
}
else if (data)
window.location.href = data;
}
// on Enter key pressed
const handleEnterKey = async function(e) {
if (e.key === 'Enter') {
e.preventDefault();
await handleLoginAsync();
}
};
// on form submit event
const handleSubmit = async function(e) {
e.preventDefault();
await handleLoginAsync();
};
// on recover password button click
const handleBtnRecoverPasswordClick = function() {
window.location.href = '@_urlService.GetAbsoluteUrl("RecoverPassword", "Auth")';
};
// add listeners
form.addEventListener('keypress', handleEnterKey);
form.addEventListener('submit', handleSubmit);
btnRecoverPassword.addEventListener('click', handleBtnRecoverPasswordClick);
// register cleanup for these listeners
cleanupFunctions.push(() => {
form.removeEventListener('keypress', handleEnterKey);
form.removeEventListener('submit', handleSubmit);
btnRecoverPassword.removeEventListener('click', handleBtnRecoverPasswordClick);
});
// 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>
}