Files
main-tbbank/backend/web/admin/assets/js/login.js
2026-08-04 12:50:20 +05:00

49 lines
1.5 KiB
JavaScript

/**
* Login page: submit credentials, store the token pair and redirect.
*/
(function () {
"use strict";
document.addEventListener("DOMContentLoaded", () => {
if (document.body.dataset.page !== "login") return;
// Already signed in? Go straight to the dashboard.
if (Api.tokens.access) {
window.location.href = "/admin";
return;
}
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);
}
});
});
})();