burgerauth/static/js/signup.js

136 lines
4.6 KiB
JavaScript
Raw Normal View History

2024-04-26 21:12:56 +01:00
if (localStorage.getItem("DONOTSHARE-secretkey") !== null) {
window.location.replace("/app" + window.location.search)
document.body.innerHTML = "Redirecting..."
throw new Error();
}
if (localStorage.getItem("DONOTSHARE-password") !== null) {
window.location.replace("/app" + window.location.search)
document.body.innerHTML = "Redirecting..."
throw new Error();
}
let remote = localStorage.getItem("homeserverURL")
if (remote == null) {
2024-04-26 21:15:43 +01:00
localStorage.setItem("homeserverURL", "https://auth.hectabit.org")
remote = "https://auth.hectabit.org"
2024-04-26 21:12:56 +01:00
}
let usernameBox = document.getElementById("usernameBox")
let passwordBox = document.getElementById("passwordBox")
let statusBox = document.getElementById("statusBox")
let signupButton = document.getElementById("signupButton")
2024-05-09 17:27:47 +01:00
let captchaBox = document.getElementById("captchaBox")
let unique_token = document.getElementById("passthrough").innerText
2024-04-26 21:12:56 +01:00
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")
}
}
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("homeserver").innerText = "Your homeserver is: " + remote + ". "
});
2024-05-09 17:27:47 +01:00
signupButton.addEventListener("click", () => {
2024-04-26 21:12:56 +01:00
async function doStuff() {
let username = usernameBox.value
let password = passwordBox.value
2024-05-09 17:27:47 +01:00
let captcha = captchaBox.value
2024-04-26 21:12:56 +01:00
2024-05-09 17:27:47 +01:00
if (username === "") {
2024-04-26 21:12:56 +01:00
statusBox.innerText = "A username is required!"
return
}
if ((username).length > 20) {
statusBox.innerText = "Username cannot be more than 20 characters!"
return
}
2024-05-09 17:27:47 +01:00
if (password === "") {
2024-04-26 21:12:56 +01:00
statusBox.innerText = "A password is required!"
return
}
if ((password).length < 8) {
statusBox.innerText = "8 or more characters are required!"
return
}
2024-05-09 17:27:47 +01:00
if (captcha === "") {
statusBox.innerText = "Please complete the captcha!"
return
}
2024-04-26 21:12:56 +01:00
showElements(false)
statusBox.innerText = "Creating account, please hold on..."
async function hashpass(pass) {
let key = pass
for (let i = 0; i < 128; i++) {
key = await hashwasm.sha3(key)
}
return key
2024-05-09 17:27:47 +01:00
}
2024-04-26 21:12:56 +01:00
fetch(remote + "/api/signup", {
method: "POST",
body: JSON.stringify({
username: username,
2024-05-09 17:27:47 +01:00
password: await hashpass(password),
captcha: captcha,
unique_token: unique_token
2024-04-26 21:12:56 +01:00
}),
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
})
.then((response) => response)
.then((response) => {
async function doStuff() {
let responseData = await response.json()
console.log(responseData)
2024-05-09 17:27:47 +01:00
if (response.status === 200) {
statusBox.innerText = "Redirecting..."
2024-04-26 21:12:56 +01:00
localStorage.setItem("DONOTSHARE-secretkey", responseData["key"])
localStorage.setItem("DONOTSHARE-password", await hashwasm.sha512(password))
window.location.href = "/app" + window.location.search
2024-05-09 16:33:01 +01:00
} else if (response.status === 409) {
2024-04-26 21:12:56 +01:00
statusBox.innerText = "Username already taken!"
showElements(true)
2024-05-09 16:33:01 +01:00
} else if (response.status === 401) {
statusBox.innerText = "CAPTCHA has expired!"
} else if (response.status === 403) {
statusBox.innerText = "CAPTCHA is incorrect!"
} else {
2024-04-26 21:12:56 +01:00
statusBox.innerText = "Something went wrong!"
showElements(true)
}
}
doStuff()
});
}
doStuff()
});
document.getElementById("loginButton").addEventListener("click", function(event) {
event.preventDefault();
2024-05-09 17:27:47 +01:00
const queryString = window.location.search;
window.location.href = "/login" + queryString;
2024-04-26 21:12:56 +01:00
});
document.getElementById("privacyButton").addEventListener("click", function(event) {
event.preventDefault();
2024-05-09 17:27:47 +01:00
const queryString = window.location.search;
window.location.href = "/privacy" + queryString;
2024-04-26 21:12:56 +01:00
});