Burgernotes/static/js/signup.js

100 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-07-21 20:52:06 +01:00
if (localStorage.getItem("DONOTSHARE-secretkey") !== null) {
2024-02-27 17:04:31 +00:00
window.location.replace("../app/index.html")
2023-11-03 20:24:40 +00:00
document.body.innerHTML = "Redirecting..."
2023-07-21 20:52:06 +01:00
throw new Error();
}
if (localStorage.getItem("DONOTSHARE-password") !== null) {
2024-02-27 17:04:31 +00:00
window.location.replace("../app/index.html")
2023-11-03 20:24:40 +00:00
document.body.innerHTML = "Redirecting..."
2023-07-21 20:52:06 +01:00
throw new Error();
}
let usernameBox = document.getElementById("usernameBox")
let passwordBox = document.getElementById("passwordBox")
let statusBox = document.getElementById("statusBox")
let signupButton = document.getElementById("signupButton")
function showElements(yesorno) {
if (!yesorno) {
usernameBox.classList.add("hidden")
passwordBox.classList.add("hidden")
signupButton.classList.add("hidden")
}
else {
usernameBox.classList.remove("hidden")
passwordBox.classList.remove("hidden")
signupButton.classList.remove("hidden")
}
}
signupButton.addEventListener("click", (event) => {
async function doStuff() {
let username = usernameBox.value
let password = passwordBox.value
if (username == "") {
2023-11-03 20:24:40 +00:00
statusBox.innerText = "A username is required!"
2023-07-21 20:52:06 +01:00
return
}
if ((username).length > 20) {
2023-11-03 20:24:40 +00:00
statusBox.innerText = "Username cannot be more than 20 characters!"
2023-07-21 20:52:06 +01:00
return
}
if (password == "") {
2023-11-03 20:24:40 +00:00
statusBox.innerText = "A password is required!"
2023-07-21 20:52:06 +01:00
return
}
2023-11-03 20:24:40 +00:00
if ((password).length < 8) {
statusBox.innerText = "8 or more characters are required!"
2023-07-21 20:52:06 +01:00
return
}
showElements(false)
2023-11-03 20:24:40 +00:00
statusBox.innerText = "Creating account, please hold on..."
2023-07-21 20:52:06 +01:00
async function hashpass(pass) {
let key = pass
for (let i = 0; i < 128; i++) {
key = await hashwasm.sha3(key)
}
2023-07-21 20:52:06 +01:00
return key
};
2024-02-27 17:04:31 +00:00
fetch("https://notes.hectabit.org/api/signup", {
2023-07-21 20:52:06 +01:00
method: "POST",
body: JSON.stringify({
username: username,
password: await hashpass(password)
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then((response) => response)
.then((response) => {
async function doStuff() {
let responseData = await response.json()
if (response.status == 200) {
statusBox.innerText == "redirecting.."
localStorage.setItem("DONOTSHARE-secretkey", responseData["key"])
localStorage.setItem("DONOTSHARE-password", await hashwasm.sha512(password))
2024-02-27 17:04:31 +00:00
window.location.href = "../app/index.html"
2023-07-21 20:52:06 +01:00
}
else if (response.status == 409) {
2023-11-03 20:24:40 +00:00
statusBox.innerText = "Username already taken!"
2023-07-21 20:52:06 +01:00
showElements(true)
}
else {
2023-11-03 20:30:32 +00:00
statusBox.innerText = "Something went wrong!"
2023-07-21 20:52:06 +01:00
showElements(true)
}
}
doStuff()
});
}
doStuff()
2023-11-03 20:24:40 +00:00
});