162 lines
6.5 KiB
Plaintext
162 lines
6.5 KiB
Plaintext
@model TwoFactorCodeViewModel
|
|
@{
|
|
ViewData["Title"] = "Two-factor authentication";
|
|
}
|
|
|
|
<div class="page-heading compact auth-page-heading">
|
|
<div>
|
|
<p class="eyebrow">Account</p>
|
|
<h1>Two-factor authentication</h1>
|
|
<p class="lead-text">Enter the current 6-digit code from your authenticator app.</p>
|
|
</div>
|
|
</div>
|
|
|
|
<section class="edit-panel two-factor-card">
|
|
<form asp-action="TwoFactor" method="post" class="asset-create-form two-factor-body" data-two-factor-form>
|
|
<input asp-for="ReturnUrl" type="hidden" />
|
|
<input asp-for="Code" type="hidden" data-two-factor-code />
|
|
<div asp-validation-summary="ModelOnly" class="alert alert-warning"></div>
|
|
|
|
<div class="two-factor-code-group">
|
|
<label class="form-label two-factor-label" for="twoFactorDigit1">Authenticator code</label>
|
|
<div class="two-factor-inputs" role="group" aria-label="Authenticator code">
|
|
@for (var i = 1; i <= 6; i++)
|
|
{
|
|
<input class="form-control two-factor-digit"
|
|
id="twoFactorDigit@(i)"
|
|
type="text"
|
|
inputmode="numeric"
|
|
pattern="[0-9]*"
|
|
maxlength="1"
|
|
autocomplete="one-time-code"
|
|
aria-label="Digit @i of 6"
|
|
data-two-factor-digit />
|
|
}
|
|
</div>
|
|
<span class="text-danger" asp-validation-for="Code"></span>
|
|
</div>
|
|
|
|
<div class="two-factor-actions">
|
|
<button class="btn btn-primary" type="submit">Continue</button>
|
|
<a class="btn btn-outline-secondary" asp-action="Login">Back to Login</a>
|
|
</div>
|
|
<p class="two-factor-help">
|
|
<span>Can't access your authenticator app?</span>
|
|
<a asp-action="TwoFactorRecovery" asp-route-returnUrl="@Model.ReturnUrl">Use a recovery code.</a>
|
|
</p>
|
|
</form>
|
|
</section>
|
|
|
|
@section Scripts {
|
|
<partial name="_ValidationScriptsPartial" />
|
|
<script>
|
|
(() => {
|
|
const form = document.querySelector("[data-two-factor-form]");
|
|
if (!form) {
|
|
return;
|
|
}
|
|
|
|
const hiddenCode = form.querySelector("[data-two-factor-code]");
|
|
const digits = Array.from(form.querySelectorAll("[data-two-factor-digit]"));
|
|
let hasSubmitted = false;
|
|
|
|
const codeValue = () => digits.map(input => input.value).join("");
|
|
const syncCode = () => {
|
|
hiddenCode.value = codeValue();
|
|
};
|
|
const isComplete = () => digits.every(input => input.value.length === 1);
|
|
const submitIfComplete = () => {
|
|
syncCode();
|
|
if (isComplete() && !hasSubmitted) {
|
|
form.requestSubmit();
|
|
}
|
|
};
|
|
const fullCode = value => {
|
|
// Only conventional digit grouping spaces are allowed internally.
|
|
const normalized = value.trim().replace(/[ \u00a0\u202f]/g, "");
|
|
return /^[0-9]{6}$/.test(normalized) ? normalized : null;
|
|
};
|
|
const fillCode = value => {
|
|
const code = fullCode(value);
|
|
if (!code) return false;
|
|
// Populate atomically: assignments do not dispatch input events.
|
|
digits.forEach((input, index) => input.value = code[index]);
|
|
syncCode();
|
|
digits[digits.length - 1].focus();
|
|
submitIfComplete();
|
|
return true;
|
|
};
|
|
const isDigit = target => digits.includes(target);
|
|
// Capture the original text before native maxlength processing. Use
|
|
// text/plain rather than relying on a browser's legacy "text" alias.
|
|
form.addEventListener("paste", event => {
|
|
if (!isDigit(event.target)) return;
|
|
const text = event.clipboardData?.getData("text/plain");
|
|
if (typeof text !== "string" || text === "") return;
|
|
event.preventDefault();
|
|
fillCode(text);
|
|
}, true);
|
|
// Native autofill and some browser/mobile insertion paths do not
|
|
// dispatch a ClipboardEvent. beforeinput still carries the full text.
|
|
form.addEventListener("beforeinput", event => {
|
|
if (!isDigit(event.target) || !event.inputType?.startsWith("insert")) return;
|
|
const text = event.dataTransfer?.getData("text/plain") || event.data;
|
|
if (typeof text !== "string" || text.length <= 1) return;
|
|
event.preventDefault();
|
|
fillCode(text);
|
|
}, true);
|
|
|
|
digits.forEach((input, index) => {
|
|
input.addEventListener("input", () => {
|
|
if (input.value.length > 1) {
|
|
if (!fillCode(input.value)) { input.value = ""; syncCode(); }
|
|
return;
|
|
}
|
|
|
|
input.value = input.value.replace(/\D/g, "");
|
|
syncCode();
|
|
|
|
if (input.value && index < digits.length - 1) {
|
|
digits[index + 1].focus();
|
|
}
|
|
|
|
submitIfComplete();
|
|
});
|
|
|
|
input.addEventListener("keydown", event => {
|
|
if (event.key === "Backspace" && !input.value && index > 0) {
|
|
digits[index - 1].focus();
|
|
digits[index - 1].value = "";
|
|
syncCode();
|
|
}
|
|
});
|
|
|
|
input.addEventListener("focus", () => input.select());
|
|
});
|
|
|
|
form.addEventListener("submit", event => {
|
|
syncCode();
|
|
if (hasSubmitted) {
|
|
event.preventDefault();
|
|
return;
|
|
}
|
|
hasSubmitted = true;
|
|
// Client validation may cancel submission. Permit correction and
|
|
// retry once all submit listeners have finished.
|
|
queueMicrotask(() => {
|
|
if (event.defaultPrevented) hasSubmitted = false;
|
|
});
|
|
});
|
|
window.addEventListener("pageshow", event => {
|
|
if (event.persisted) hasSubmitted = false;
|
|
});
|
|
|
|
digits.forEach(input => {
|
|
input.value = "";
|
|
});
|
|
syncCode();
|
|
digits[0].focus();
|
|
})();
|
|
</script>
|
|
}
|