35 lines
1 KiB
TypeScript
35 lines
1 KiB
TypeScript
import { login } from "./api";
|
|
|
|
const form = document.querySelector("form")!;
|
|
const passwordInput = document.getElementById("password") as HTMLInputElement;
|
|
|
|
const errorEl = document.createElement("p");
|
|
errorEl.className = "text-red-500 mt-2 text-sm hidden";
|
|
form.appendChild(errorEl);
|
|
|
|
form.addEventListener("submit", async (e) => {
|
|
e.preventDefault();
|
|
errorEl.classList.add("hidden");
|
|
|
|
const password = passwordInput.value.trim();
|
|
if (!password) {
|
|
errorEl.textContent = "Please enter a password.";
|
|
errorEl.classList.remove("hidden");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await login(password);
|
|
|
|
if (res.success) {
|
|
localStorage.setItem("password", password);
|
|
window.location.href = "/dashboard.html";
|
|
} else {
|
|
errorEl.textContent = res.error || "Invalid password.";
|
|
errorEl.classList.remove("hidden");
|
|
}
|
|
} catch {
|
|
errorEl.textContent = "Network error. Please try again.";
|
|
errorEl.classList.remove("hidden");
|
|
}
|
|
});
|