/* Preview behavior only. Keycloak will supply the real authentication later. */ "use strict"; const $ = (selector) => document.querySelector(selector); const screens = { login: ["Sign in", ""], otp: ["Verify identity", ""], reset: ["Reset password", "We’ll send a reset link to your email."], success: ["Access granted", ""], }; function notice(message = "", error = false) { $("#notice").textContent = message; $("#notice").hidden = !message; $("#notice").classList.toggle("error", error); } function showScreen(requested, focus = false) { const screen = requested === "error" ? "login" : (Object.hasOwn(screens, requested) ? requested : "login"); const [title, description] = screens[screen]; document.querySelectorAll("[data-view]").forEach((element) => { element.hidden = element.dataset.view !== screen; }); $("#page-title").replaceChildren(document.createTextNode(title)); const cursor = document.createElement("span"); cursor.className = "cursor"; cursor.setAttribute("aria-hidden", "true"); cursor.textContent = "_"; $("#page-title").append(cursor); $("#page-description").textContent = description; $("#page-description").hidden = !description; document.querySelectorAll("[data-screen]").forEach((button) => button.setAttribute("aria-pressed", String(button.dataset.screen === requested))); document.querySelectorAll(".field-error").forEach((element) => { element.hidden = true; }); document.querySelectorAll("[aria-invalid]").forEach((element) => element.removeAttribute("aria-invalid")); notice(requested === "error" ? "[ DENIED ] Invalid username or password. Please try again." : "", requested === "error"); $("#password").value = ""; $("#password").type = "password"; $("#reveal-password").textContent = "SHOW"; $("#reveal-password").setAttribute("aria-label", "Show password"); $("#reveal-password").setAttribute("aria-pressed", "false"); $("#otp").value = ""; if (focus) document.querySelector(`[data-view="${screen}"] input`)?.focus(); } function navigate(screen) { if (location.hash === `#${screen}`) showScreen(screen, true); else location.hash = screen; } function fieldError(field, output, message) { field.setAttribute("aria-invalid", "true"); output.textContent = message; output.hidden = false; field.focus(); } document.querySelectorAll("[data-screen]").forEach((button) => button.addEventListener("click", () => navigate(button.dataset.screen))); window.addEventListener("hashchange", () => showScreen(location.hash.slice(1), true)); $("#login-form").addEventListener("submit", (event) => { event.preventDefault(); notice(); if (!$("#username").value.trim()) return fieldError($("#username"), $("#login-error"), "Enter your username or email address."); if (!$("#password").value) return fieldError($("#password"), $("#login-error"), "Enter your password."); $("#password").value = ""; navigate("otp"); }); $("#otp-form").addEventListener("submit", (event) => { event.preventDefault(); if (!/^[0-9]{6}$/.test($("#otp").value)) return fieldError($("#otp"), $("#otp-error"), "Enter a 6-digit verification code."); $("#otp").value = ""; navigate("success"); }); $("#reset-form").addEventListener("submit", (event) => { event.preventDefault(); if (!$("#reset-email").validity.valid) return fieldError($("#reset-email"), $("#reset-error"), "Enter a valid email address."); $("#reset-error").hidden = true; $("#reset-email").removeAttribute("aria-invalid"); notice("[ OK ] Reset-link confirmation preview. No email was sent."); }); $("#reveal-password").addEventListener("click", () => { const reveal = $("#password").type === "password"; $("#password").type = reveal ? "text" : "password"; $("#reveal-password").textContent = reveal ? "HIDE" : "SHOW"; $("#reveal-password").setAttribute("aria-label", reveal ? "Hide password" : "Show password"); $("#reveal-password").setAttribute("aria-pressed", String(reveal)); }); $("#passkey").addEventListener("click", () => notice("[ DEMO ] Passkey authentication will be handled by Keycloak. No device prompt is opened here.")); $("#google").addEventListener("click", () => notice("[ DEMO ] Google sign-in will be handled by Keycloak. This preview stays local.")); document.querySelectorAll("input").forEach((input) => input.addEventListener("input", () => { input.removeAttribute("aria-invalid"); input.closest("form")?.querySelectorAll(".field-error").forEach((element) => { element.hidden = true; }); })); showScreen(location.hash.slice(1)); // The optional stdlib server reloads the page when HTML/CSS/JS files change. if (location.protocol === "http:" && ["127.0.0.1", "localhost"].includes(location.hostname)) { let version; let checking = false; setInterval(async () => { if (checking || document.hidden) return; checking = true; try { const response = await fetch("/__version", { cache: "no-store" }); if (!response.ok || !response.headers.get("content-type")?.includes("application/json")) return; const current = (await response.json()).version; if (version !== undefined && current !== version) location.reload(); version = current; } catch { /* The preview also works without the local server. */ } finally { checking = false; } }, 1000); }