infopage/public/js/signup.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-02-20 14:55:22 -05:00
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;
2024-03-28 21:36:08 -04:00
password = sha256(dom("password").value);
if (sha256(dom("confirm").value) != password) {
showlog("Passwords don't match!");
return;
}
this.setAttribute("disabled", "");
2024-02-20 14:55:22 -05:00
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}`);
}
})
2024-02-20 14:55:22 -05:00
})
.catch((err) => {
dom("submit").removeAttribute("disabled");
showlog("Something went wrong! Try again in a few moments...");
throw(err);
});
2024-03-28 21:36:08 -04:00
});