Login
This commit is contained in:
41
static/login.js
Normal file
41
static/login.js
Normal file
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user