From 7fbe592529a3016994c88f3e778b62d7e48a6565 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 7 Apr 2023 21:15:33 +0100 Subject: [PATCH] Debugging --- js/auth.js | 26 +++++++++++++------------- js/index.js | 1 - js/settings.js | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/js/auth.js b/js/auth.js index 6e5608c..33e3d95 100644 --- a/js/auth.js +++ b/js/auth.js @@ -19,21 +19,19 @@ async function cmdOut(message) { } 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 + const url = `${window.location.origin}/api/v1/register/register`; + const 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) + body: JSON.stringify({uuid: key}) }) - if (res.JSON['result'] == "ok"){ - localStorage.setItem("uuid", res.JSON['uuid']); - return true; - } else { - return false; - } + const data = await res.json(); + return res.status === 201 + ? (localStorage.setItem("uuid", data.uuid), true) + : false; } async function checkAuth(key) { @@ -53,10 +51,12 @@ async function init(){ // Reads registration key from query, and calls registerK const key = await getQuery("key"); cmdOut(`Temporary: ${key}`) cmdOut("Requesting API Key from server"); - let res = await registerKey(key); - console.log(JSON.stringify(res)) - cmdOut("Checking key validity") - checkAuth(localStorage.getItem("uuid")) + if (!await registerKey(key)) { + cmdOut("Failed to register key") + hideLoading() + return + } + await checkAuth(localStorage.getItem("uuid")) ? cmdOut("Authentication succesful") : cmdOut("Authentication failed") } \ No newline at end of file diff --git a/js/index.js b/js/index.js index 3eb9f67..50b4817 100644 --- a/js/index.js +++ b/js/index.js @@ -9,7 +9,6 @@ if ("serviceWorker" in navigator) { async function pageInit() { await loadQuickLinks(); hideLoading(); // From lib.main - localStorage.setItem("ver-web", document.getElementById('ver_str').textContent) } async function gotoBoard(station){ diff --git a/js/settings.js b/js/settings.js index 909e068..5bfcded 100644 --- a/js/settings.js +++ b/js/settings.js @@ -78,7 +78,7 @@ async function register() { // Registers a device by sending POST request to API if (! await isRegistered()) { showLoading() let url = `${window.location.origin}/api/v1/register/request`; - let email = document.getElementById("eml").textContent + let email = document.getElementById("eml").value let res = await fetch(url, { method: "POST", headers: {