44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
const siteForm = document.querySelector("#publicSignupForm");
|
|
const siteResult = document.querySelector("#signupResult");
|
|
|
|
function showSignupMessage(message) {
|
|
if (siteResult) {
|
|
siteResult.textContent = message;
|
|
}
|
|
}
|
|
|
|
async function siteApi(action, payload) {
|
|
const response = await fetch(`/api.php?action=${encodeURIComponent(action)}`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
const json = await response.json();
|
|
if (!response.ok || json.error) {
|
|
throw new Error(json.error || "Request failed.");
|
|
}
|
|
return json;
|
|
}
|
|
|
|
if (siteForm) {
|
|
siteForm.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
const payload = Object.fromEntries(new FormData(siteForm).entries());
|
|
showSignupMessage("Creating profile...");
|
|
try {
|
|
const result = await siteApi("public_signup", payload);
|
|
if (result.user?.id) {
|
|
localStorage.setItem("nht:userId", String(result.user.id));
|
|
}
|
|
siteForm.reset();
|
|
const adminCopy = result.first_admin ? " You are the first user, so this profile is an admin." : "";
|
|
showSignupMessage(`Profile created.${adminCopy} Opening tracker...`);
|
|
window.setTimeout(() => {
|
|
window.location.href = "/tracker.php";
|
|
}, 900);
|
|
} catch (error) {
|
|
showSignupMessage(error.message);
|
|
}
|
|
});
|
|
}
|