Burgernotes/static/js/login.js

190 lines
7.3 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")
2023-10-13 19:00:34 +01:00
let inputNameBox = document.getElementById("inputNameBox")
let backButton = document.getElementById("backButton")
usernameBox.classList.remove("hidden")
2023-11-03 20:24:40 +00:00
inputNameBox.innerText = "Username:"
2023-10-13 19:00:34 +01:00
let currentInputType = 0
function showInput(inputType) {
if (inputType == 0) {
usernameBox.classList.remove("hidden")
passwordBox.classList.add("hidden")
backButton.classList.add("hidden")
2023-11-03 20:24:40 +00:00
inputNameBox.innerText = "Username:"
2024-02-26 17:25:59 +00:00
statusBox.innerText = "Login to your Burgernotes account!"
2023-10-13 19:00:34 +01:00
currentInputType = 0
} else if (inputType == 1) {
usernameBox.classList.add("hidden")
passwordBox.classList.remove("hidden")
backButton.classList.remove("hidden")
2023-11-03 20:24:40 +00:00
inputNameBox.innerText = "Password:"
2023-10-13 19:00:34 +01:00
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")
2023-11-03 20:24:40 +00:00
inputNameBox.innerText = "Password:"
2023-10-13 19:00:34 +01:00
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-11-03 20:24:40 +00:00
statusBox.innerText = "A username is required!"
2023-07-22 17:15:59 +01:00
return
2023-10-13 19:00:34 +01:00
} else {
2023-11-03 20:24:40 +00:00
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 == "") {
2023-11-03 20:24:40 +00:00
statusBox.innerText = "A password is required!"
2023-10-13 19:00:34 +01:00
return
}
2023-07-21 20:52:06 +01:00
2023-10-13 19:00:34 +01:00
showInput(2)
showElements(true)
2023-11-03 20:24:40 +00:00
statusBox.innerText = "Signing in..."
2023-07-21 20:52:06 +01:00
2024-02-25 18:25:31 +00:00
async function hashpassold(pass) {
2023-10-13 19:00:34 +01:00
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
2024-02-25 18:25:31 +00:00
async function hashpass(pass) {
let key = pass
for (let i = 0; i < 128; i++) {
key = await hashwasm.sha3(key)
}
2024-02-25 18:25:31 +00:00
return key
};
2024-02-27 17:04:31 +00:00
fetch("https://notes.hectabit.org/api/login", {
2023-10-13 19:00:34 +01:00
method: "POST",
body: JSON.stringify({
username: username,
2024-02-25 19:05:22 +00:00
password: await hashpass(password),
2024-02-25 19:21:26 +00:00
passwordchange: "no",
newpass: "null"
}),
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
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))
2024-02-27 17:04:31 +00:00
window.location.href = "../app/index.html"
2023-10-13 19:00:34 +01:00
}
else if (response.status == 401) {
2024-02-25 18:27:35 +00:00
console.log("Trying oldhash")
2024-02-27 17:04:31 +00:00
fetch("https://notes.hectabit.org/api/login", {
2024-02-25 18:25:31 +00:00
method: "POST",
body: JSON.stringify({
username: username,
2024-02-25 19:05:22 +00:00
password: await hashpassold(password),
2024-02-25 19:21:26 +00:00
passwordchange: "yes",
2024-02-25 19:05:22 +00:00
newpass: await hashpass(password)
}),
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
2024-02-25 18:25:31 +00:00
})
.then((response) => response)
.then((response) => {
2024-02-25 18:27:35 +00:00
async function doStuff2() {
2024-02-25 18:25:31 +00:00
let responseData = await response.json()
if (response.status == 200) {
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"
2024-02-25 18:25:31 +00:00
}
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)
}
}
2024-02-25 18:27:35 +00:00
doStuff2()
2024-02-25 18:25:31 +00:00
});
2023-10-13 19:00:34 +01:00
}
else {
2023-11-03 20:24:40 +00:00
statusBox.innerText = "Something went wrong! (error code: " + response.status + ")"
2023-10-13 19:00:34 +01:00
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)
});
2023-11-03 20:24:40 +00:00
showInput(0)