From 55831164ed47eb30e5b5dd58a420f0710072d845 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 24 Aug 2023 13:32:12 +0100 Subject: [PATCH] Add libauth.ts for centralised authentication --- src/lib/libauth.ts | 60 ++++++++++++++++++++++++++++++++ src/routes/more/reg/+page.svelte | 16 ++++----- 2 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 src/lib/libauth.ts diff --git a/src/lib/libauth.ts b/src/lib/libauth.ts new file mode 100644 index 0000000..37b213a --- /dev/null +++ b/src/lib/libauth.ts @@ -0,0 +1,60 @@ +import { uuid } from "./stores/uuid" + +export interface libauthResponse { + uuidPresent?: boolean; + serverAuthCheck?: boolean; + uuidValue?: string; + serverAuthCheckResponseCode?: number; +} + +export async function checkAuth(): Promise { + let result: libauthResponse = {}; + const uuidCheck = await checkUuid(); + result.uuidPresent = uuidCheck.uuidPresent; + result.uuidValue = uuidCheck.uuidValue; + + const serverCheck = await checkServerAuth(); + result.serverAuthCheck = serverCheck.authOk; + result.serverAuthCheckResponseCode = serverCheck.status; + + return result +} + +export async function checkUuid() { + let uuid_value: string = ''; + uuid.subscribe((value => uuid_value = value)) + console.log("uuid-value is: ", uuid_value) + if (uuid_value && uuid_value != 'null') { + return { + uuidPresent: true, + uuidValue: uuid_value, + } + } + return { + uuidPresent: false, + uuidValue: '', + } +} + +export async function checkServerAuth() { + let uuid_value: string = ''; + uuid.subscribe((value => uuid_value = value)) + const url = "https://owlboard.info/api/v2/auth/check" + const options = { + method: 'GET', + headers: { + uuid: uuid_value, + } + }; + const res = await fetch(url, options) + let ok: boolean; + if (res.status === 200) { + ok = true; + } else { + ok = false; + } + return { + authOk: ok, + status: res.status, + } +} \ No newline at end of file diff --git a/src/routes/more/reg/+page.svelte b/src/routes/more/reg/+page.svelte index d5c103b..0dee6c3 100644 --- a/src/routes/more/reg/+page.svelte +++ b/src/routes/more/reg/+page.svelte @@ -4,6 +4,7 @@ import Loading from '$lib/navigation/loading.svelte'; import { onMount } from 'svelte'; import { uuid } from '$lib/stores/uuid.js'; + import { checkAuth } from '$lib/libauth'; const title = 'Register'; @@ -38,17 +39,14 @@ isLoading = false; } - $: { - if ($uuid != 'null') { - state = 'reg' - } else state = 'unreg' - } - - /* onMount(async () => { - if ($uuid != 'null') { + onMount(async () => { + const auth = await checkAuth(); + if (auth.uuidPresent === false) { + state = 'unreg'; + } else if (auth.uuidPresent === true) { state = 'reg'; } - });*/ + }); {#if isLoading}