42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
const form = document.getElementById("login-form");
|
|
const passwordInput = document.getElementById("password");
|
|
const errorEl = document.getElementById("login-error");
|
|
|
|
// If already logged in, skip straight to the app.
|
|
fetch("/api/me").then((res) => {
|
|
if (res.ok) window.location.href = "/";
|
|
});
|
|
|
|
form.addEventListener("submit", async (e) => {
|
|
e.preventDefault();
|
|
errorEl.textContent = "";
|
|
|
|
const submitBtn = form.querySelector(".login-submit");
|
|
submitBtn.disabled = true;
|
|
|
|
try {
|
|
const res = await fetch("/api/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ password: passwordInput.value }),
|
|
});
|
|
|
|
if (res.ok) {
|
|
window.location.href = "/";
|
|
return;
|
|
}
|
|
|
|
if (res.status === 429) {
|
|
errorEl.textContent = "Zu viele Fehlversuche. Bitte kurz warten.";
|
|
} else {
|
|
errorEl.textContent = "Falsches Passwort.";
|
|
}
|
|
passwordInput.value = "";
|
|
passwordInput.focus();
|
|
} catch (err) {
|
|
errorEl.textContent = "Verbindung fehlgeschlagen.";
|
|
} finally {
|
|
submitBtn.disabled = false;
|
|
}
|
|
});
|