Files
main-tbbank/backend/web/admin/assets/js/login.js
GuwandykA 5f2e2f44ca Fix admin login reload loop from stale localStorage tokens.
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>
2026-08-07 11:20:22 +05:00

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);
}
});
});
})();