photo_server_json_con_aves22/public/js/login.js
2026-04-18 20:14:42 +02:00

64 lines
No EOL
1.6 KiB
JavaScript

// ===============================
// LOGIN.JS — Gestione form login
// ===============================
(() => {
const form = document.getElementById("loginForm");
const userInput = document.getElementById("loginUser");
const passInput = document.getElementById("loginPass");
const submitBtn = document.getElementById("loginSubmit");
const errorBox = document.getElementById("loginError");
const redirectUrl =
form?.getAttribute("data-redirect") ||
new URLSearchParams(location.search).get("redirect") ||
"/";
function showError(msg) {
if (!errorBox) return;
errorBox.textContent = msg;
errorBox.classList.add("visible");
}
function clearError() {
if (!errorBox) return;
errorBox.textContent = "";
errorBox.classList.remove("visible");
}
function setLoading(loading) {
submitBtn.disabled = loading;
submitBtn.setAttribute("aria-busy", loading ? "true" : "false");
}
async function handleSubmit(e) {
e.preventDefault();
clearError();
const email = userInput.value.trim();
const password = passInput.value;
if (!email || !password) {
showError("Inserisci email e password");
return;
}
setLoading(true);
try {
await window.AppAuth.login(email, password);
window.location.assign(redirectUrl);
} catch (err) {
showError("Credenziali non valide");
} finally {
setLoading(false);
}
}
function init() {
if (!form) return;
form.addEventListener("submit", handleSubmit);
}
document.addEventListener("DOMContentLoaded", init);
})();