2023-04-05 16:27:49 +01:00
|
|
|
/*
|
|
|
|
Auth process: User Requests Key => Server emails key to user =>
|
2023-04-05 21:35:43 +01:00
|
|
|
user opens link to auth.html =>
|
2023-04-05 16:27:49 +01:00
|
|
|
website POSTs key to server => Server checks validity =>
|
|
|
|
Server responds with the users auth key =>
|
|
|
|
auth.js adds this to localStorage
|
|
|
|
*/
|
|
|
|
const cmd = document.getElementById("cmd") // Assign element to const
|
|
|
|
init(); // Run init function
|
2023-04-04 20:31:21 +01:00
|
|
|
|
|
|
|
async function sendHome(){
|
|
|
|
await delay(2000);
|
|
|
|
location.replace('/')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cmdOut(message) {
|
|
|
|
html = "<p>" + message + "</p>"
|
|
|
|
cmd.insertAdjacentHTML("beforeend", html)
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:27:49 +01:00
|
|
|
async function registerKey(key) { // Posts key to server and listens for response.
|
2023-04-05 21:25:28 +01:00
|
|
|
var url = `${window.location.origin}/api/v1/register/register`;
|
2023-04-05 16:27:49 +01:00
|
|
|
let res = await fetch(url, { // The response will contain the UUID which will be registered
|
2023-04-04 20:31:21 +01:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
},
|
|
|
|
redirect: "follow",
|
|
|
|
body: JSON.stringify(key)
|
|
|
|
})
|
|
|
|
if (res.JSON['result'] == "ok"){
|
2023-04-06 20:46:25 +01:00
|
|
|
localStorage.setItem("uuid", res.JSON['uuid']);
|
2023-04-04 20:31:21 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 18:59:36 +01:00
|
|
|
async function checkAuth(key) {
|
|
|
|
const url = `${window.location.origin}/api/v1/auth/test`;
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: "GET",
|
|
|
|
redirect: "follow",
|
|
|
|
headers: {
|
|
|
|
"uuid": key
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return res.status === 200 ? true : false
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:27:49 +01:00
|
|
|
async function init(){ // Reads registration key from query, and calls registerKey(key)
|
2023-04-04 20:31:21 +01:00
|
|
|
cmdOut("Reading registration key");
|
|
|
|
const key = await getQuery("key");
|
2023-04-07 18:59:36 +01:00
|
|
|
cmdOut(`Temporary: ${key}`)
|
2023-04-04 20:31:21 +01:00
|
|
|
cmdOut("Requesting API Key from server");
|
|
|
|
let res = await registerKey(key);
|
|
|
|
console.log(JSON.stringify(res))
|
2023-04-07 18:59:36 +01:00
|
|
|
cmdOut("Checking key validity")
|
|
|
|
checkAuth(localStorage.getItem("uuid"))
|
|
|
|
? cmdOut("Authentication succesful")
|
|
|
|
: cmdOut("Authentication failed")
|
2023-04-04 20:31:21 +01:00
|
|
|
}
|