Clear client tokens on the login page, gate admin pages with shared cookie JWT checks, and clear Secure cookies reliably behind the proxy. Co-authored-by: Cursor <cursoragent@cursor.com>
52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
/**
|
|
* Login page: submit credentials; server sets cookies and we redirect.
|
|
*
|
|
* Do not bounce to /admin based on localStorage. Stale client tokens caused a
|
|
* reload loop with cookie-gated AdminPageAuth (login → /admin → login).
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
if (document.body.dataset.page !== "login") return;
|
|
|
|
// Drop any legacy localStorage session. Auth is HttpOnly cookies + server
|
|
// AdminGuestOnly; a leftover access token must not auto-navigate to /admin.
|
|
if (window.Api && Api.tokens) {
|
|
Api.tokens.clear();
|
|
}
|
|
|
|
const form = document.getElementById("login-form");
|
|
const loginInput = document.getElementById("login-input");
|
|
const passwordInput = document.getElementById("password-input");
|
|
const errorEl = document.getElementById("login-error");
|
|
const submitButton = document.getElementById("login-submit");
|
|
|
|
form.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
errorEl.classList.add("hidden");
|
|
|
|
const loginValue = loginInput.value.trim();
|
|
const password = passwordInput.value;
|
|
if (!loginValue || !password) {
|
|
errorEl.textContent = "Please enter your login and password.";
|
|
errorEl.classList.remove("hidden");
|
|
return;
|
|
}
|
|
|
|
UI.setLoading(submitButton, true);
|
|
try {
|
|
await Auth.login(loginValue, password);
|
|
window.location.href = "/admin";
|
|
} catch (error) {
|
|
errorEl.textContent =
|
|
error.status === 400
|
|
? "Invalid login or password."
|
|
: error.message;
|
|
errorEl.classList.remove("hidden");
|
|
UI.setLoading(submitButton, false);
|
|
}
|
|
});
|
|
});
|
|
})();
|