infopage/public/js/signup.js
2024-02-26 17:35:18 -05:00

54 lines
No EOL
1.6 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() {
this.setAttribute("disabled", "");
username = dom("username").value;
password = sha256(dom("password").value)
fetch("/api/account/signup", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"username": username,
"password": password
})
})
.then((response) => {
if (response.status == 429) {
dom("post").removeAttribute("disabled");
dom("post-text").removeAttribute("disabled");
showlog("You are being ratelimited! Try again in a few moments...");
} else {
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);
});
});