56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
(() => {
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
const form = document.getElementById("login-form");
|
|
const input = document.getElementById("pw");
|
|
if (!form || !input) return;
|
|
const invalidPw = form.getAttribute("data-invalid-pw") || "Invalid password";
|
|
const connFailed = form.getAttribute("data-conn-failed") || "Connection failed";
|
|
function showErr(msg) {
|
|
const err = document.getElementById("err");
|
|
if (err) {
|
|
err.textContent = msg;
|
|
err.style.display = "block";
|
|
}
|
|
}
|
|
function hideErr() {
|
|
const err = document.getElementById("err");
|
|
if (err) {
|
|
err.style.display = "none";
|
|
}
|
|
}
|
|
async function doLogin(e) {
|
|
e.preventDefault();
|
|
const pw = input.value;
|
|
hideErr();
|
|
try {
|
|
const res = await fetch("api/auth/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ password: pw }),
|
|
credentials: "include"
|
|
});
|
|
let data = {};
|
|
try {
|
|
data = await res.json();
|
|
} catch (_) {
|
|
}
|
|
if (res.ok && data.ok) {
|
|
window.location.href = "./";
|
|
} else {
|
|
showErr(data.error || invalidPw);
|
|
}
|
|
} catch (ex) {
|
|
showErr(connFailed);
|
|
}
|
|
}
|
|
form.addEventListener("submit", doLogin);
|
|
input.addEventListener("keydown", function(e) {
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
doLogin(e);
|
|
}
|
|
});
|
|
});
|
|
})();
|
|
//# sourceMappingURL=login.js.map
|