liftman/public/auth.js
2026-05-22 23:05:37 +02:00

43 lines
1.1 KiB
JavaScript

const form = document.getElementById("authForm");
const feedback = document.getElementById("authMessage");
const isRegisterPage = window.location.pathname === "/regisztracio";
form?.addEventListener("submit", handleSubmit);
async function handleSubmit(event) {
event.preventDefault();
const formData = new FormData(form);
const payload = Object.fromEntries(formData.entries());
const endpoint = isRegisterPage ? "/api/register" : "/api/login";
try {
const response = await fetch(endpoint, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.message || "A kérés nem sikerült.");
}
setFeedback(data.message, "success");
window.setTimeout(() => {
window.location.href = data.redirectPath;
}, 500);
} catch (error) {
setFeedback(error.message, "error");
}
}
function setFeedback(message, type) {
feedback.textContent = message;
feedback.className = `feedback-text ${type ? `feedback-${type}` : ""}`;
}