infopage/public/js/signup.js
2024-03-28 21:36:08 -04:00

54 lines
1.5 KiB
JavaScript

let inc = 0, req = 0;
showlog = (str, time=3000) => {
inc++;
dom("error").innerText = str;
setTimeout(() => { req++; if (req == inc) { dom("error").innerText = ""; }}, time);
};
dom("toggle-password").addEventListener("click", function() {
if (dom("password").getAttribute("type") == "password") {
dom("password").setAttribute("type", "text");
} else {
dom("password").setAttribute("type", "password");
}
});
dom("submit").addEventListener("click", function() {
username = dom("username").value;
password = sha256(dom("password").value);
if (sha256(dom("confirm").value) != password) {
showlog("Passwords don't match!");
return;
}
this.setAttribute("disabled", "");
fetch("/api/account/signup", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"username": username,
"password": password
})
})
.then((response) => {
response.json().then((json) => {
if (json.valid) {
setCookie("token", json.token);
localStorage.setItem("token", json.token);
window.location.href = "/home";
} else {
dom("submit").removeAttribute("disabled");
showlog(`Unable to create account! Reason: ${json.reason}`);
}
})
})
.catch((err) => {
dom("submit").removeAttribute("disabled");
showlog("Something went wrong! Try again in a few moments...");
throw(err);
});
});