Burgernotes/static/js/login.js

146 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-07-21 20:52:06 +01:00
if (localStorage.getItem("DONOTSHARE-secretkey") !== null) {
window.location.replace("/app")
2023-08-02 20:08:11 +01:00
document.body.innerHTML = "Redirecting.."
2023-07-21 20:52:06 +01:00
throw new Error();
}
if (localStorage.getItem("DONOTSHARE-password") !== null) {
window.location.replace("/app")
2023-08-02 20:08:11 +01: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")
2023-10-13 19:00:34 +01:00
let inputNameBox = document.getElementById("inputNameBox")
let backButton = document.getElementById("backButton")
usernameBox.classList.remove("hidden")
inputNameBox.innerText = "username:"
let currentInputType = 0
function showInput(inputType) {
if (inputType == 0) {
usernameBox.classList.remove("hidden")
passwordBox.classList.add("hidden")
backButton.classList.add("hidden")
inputNameBox.innerText = "username:"
statusBox.innerText = "log in to your burgernotes account!"
currentInputType = 0
} else if (inputType == 1) {
usernameBox.classList.add("hidden")
passwordBox.classList.remove("hidden")
backButton.classList.remove("hidden")
inputNameBox.innerText = "password:"
currentInputType = 1
} else if (inputType == 2) {
usernameBox.classList.add("hidden")
passwordBox.classList.add("hidden")
signupButton.classList.add("hidden")
backButton.classList.add("hidden")
inputNameBox.classList.add("hidden")
inputNameBox.innerText = "password:"
currentInputType = 2
}
}
2023-07-21 20:52:06 +01:00
function showElements(yesorno) {
if (!yesorno) {
usernameBox.classList.add("hidden")
passwordBox.classList.add("hidden")
signupButton.classList.add("hidden")
2023-10-13 19:00:34 +01:00
backButton.classList.add("hidden")
inputNameBox.classList.add("hidden")
showInput(currentInputType)
2023-07-21 20:52:06 +01:00
}
else {
usernameBox.classList.remove("hidden")
passwordBox.classList.remove("hidden")
signupButton.classList.remove("hidden")
2023-10-13 19:00:34 +01:00
backButton.classList.remove("hidden")
inputNameBox.classList.remove("hidden")
showInput(currentInputType)
2023-07-21 20:52:06 +01:00
}
}
signupButton.addEventListener("click", (event) => {
2023-10-13 19:00:34 +01:00
if (passwordBox.classList.contains("hidden")) {
if (usernameBox.value == "") {
2023-07-22 17:15:59 +01:00
statusBox.innerText = "username required"
return
2023-10-13 19:00:34 +01:00
} else {
statusBox.innerText = "welcome back, " + usernameBox.value + "!"
2023-07-22 17:15:59 +01:00
}
2023-10-13 19:00:34 +01:00
showInput(1)
} else {
async function doStuff() {
let username = usernameBox.value
let password = passwordBox.value
2023-07-22 17:15:59 +01:00
2023-10-13 19:00:34 +01:00
if (password == "") {
statusBox.innerText = "password required"
return
}
2023-07-21 20:52:06 +01:00
2023-10-13 19:00:34 +01:00
showInput(2)
showElements(true)
statusBox.innerText = "signing in.."
2023-07-21 20:52:06 +01:00
2023-10-13 19:00:34 +01:00
async function hashpass(pass) {
const key = await hashwasm.argon2id({
password: pass,
salt: await hashwasm.sha512(pass),
parallelism: 1,
iterations: 256,
memorySize: 512,
hashLength: 32,
outputType: "encoded"
});
return key
};
2023-07-21 20:52:06 +01:00
2023-10-13 19:00:34 +01:00
fetch("/api/login", {
method: "POST",
body: JSON.stringify({
username: username,
password: await hashpass(password)
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
2023-07-21 20:52:06 +01:00
}
2023-10-13 19:00:34 +01:00
})
.then((response) => response)
.then((response) => {
async function doStuff() {
let responseData = await response.json()
if (response.status == 200) {
localStorage.setItem("DONOTSHARE-secretkey", responseData["key"])
localStorage.setItem("DONOTSHARE-password", await hashwasm.sha512(password))
window.location.href = "/app"
}
else if (response.status == 401) {
statusBox.innerText = "wrong username or password :("
showInput(1)
showElements(true)
}
else {
statusBox.innerText = "something went wrong! (error code: " + response.status + ")"
showInput(1)
showElements(true)
}
}
doStuff()
});
}
doStuff()
2023-07-21 20:52:06 +01:00
}
2023-10-13 19:00:34 +01:00
});
backButton.addEventListener("click", (event) => {
showInput(0)
});
showInput(0)