This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
web/js/auth.js

46 lines
1.4 KiB
JavaScript

/*
Auth process: User Requests Key => Server emails key to user =>
user opens link on auth.html =>
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
async function sendHome(){
await delay(2000);
location.replace('/')
}
async function cmdOut(message) {
html = "<p>" + message + "</p>"
cmd.insertAdjacentHTML("beforeend", html)
}
async function registerKey(key) { // Posts key to server and listens for response.
var url = `${window.location.origin}/api/v1/register/register`;
let res = await fetch(url, { // The response will contain the UUID which will be registered
method: "POST",
headers: {
"Content-Type": "application/json"
},
redirect: "follow",
body: JSON.stringify(key)
})
if (res.JSON['result'] == "ok"){
localStorage.setItem("uuid", res.JSON['uuid'])
return true;
} else {
return false;
}
}
async function init(){ // Reads registration key from query, and calls registerKey(key)
cmdOut("Reading registration key");
const key = await getQuery("key");
cmdOut(`Key: ${key}`)
cmdOut("Requesting API Key from server");
let res = await registerKey(key);
console.log(JSON.stringify(res))
}