77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
async function pidRequireAuth() {
|
|
try {
|
|
return await pidApiRequest("/me/");
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function pidLogout() {
|
|
try {
|
|
await pidApiRequest("/auth/logout/", { method: "POST" });
|
|
} catch (e) {
|
|
// mesmo se a chamada falhar, força a saída no cliente
|
|
}
|
|
window.location.href = "index.html";
|
|
}
|
|
|
|
function pidBindPasswordToggle(toggleBtn, input) {
|
|
if (!toggleBtn || !input) return;
|
|
toggleBtn.addEventListener("click", () => {
|
|
const isPassword = input.type === "password";
|
|
input.type = isPassword ? "text" : "password";
|
|
toggleBtn.setAttribute("aria-label", isPassword ? "Ocultar senha" : "Mostrar senha");
|
|
});
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
const form = document.getElementById("login-form");
|
|
if (!form) return;
|
|
|
|
try {
|
|
const me = await pidApiRequest("/me/", { redirectOn401: false });
|
|
if (me) {
|
|
window.location.href = "portal.html";
|
|
return;
|
|
}
|
|
} catch (e) {
|
|
// sem sessão ativa — permanece no login
|
|
}
|
|
|
|
const userInput = document.getElementById("login-user");
|
|
const passInput = document.getElementById("login-pass");
|
|
const errorEl = document.getElementById("login-error");
|
|
const toggleBtn = document.getElementById("toggle-password");
|
|
const submitBtn = form.querySelector('button[type="submit"]');
|
|
|
|
pidBindPasswordToggle(toggleBtn, passInput);
|
|
|
|
form.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
const username = userInput.value.trim();
|
|
const password = passInput.value.trim();
|
|
|
|
if (!username || !password) {
|
|
errorEl.textContent = "Informe login e senha para continuar.";
|
|
return;
|
|
}
|
|
|
|
errorEl.textContent = "";
|
|
if (submitBtn) submitBtn.disabled = true;
|
|
try {
|
|
await pidApiRequest("/auth/login/", {
|
|
method: "POST",
|
|
redirectOn401: false,
|
|
body: { username, password },
|
|
});
|
|
pidPlayLoginIntro(() => {
|
|
sessionStorage.setItem("pid_reveal_portal", "1");
|
|
window.location.href = "portal.html";
|
|
});
|
|
} catch (e) {
|
|
errorEl.textContent = e.message || "Login ou senha inválidos.";
|
|
if (submitBtn) submitBtn.disabled = false;
|
|
}
|
|
});
|
|
});
|