diff options
Diffstat (limited to 'web/pw-server/src/lib')
-rw-r--r-- | web/pw-server/src/lib/components/RulesView.svelte | 1 | ||||
-rw-r--r-- | web/pw-server/src/lib/components/navbar/UserControls.svelte | 3 | ||||
-rw-r--r-- | web/pw-server/src/lib/utils.ts | 35 |
3 files changed, 37 insertions, 2 deletions
diff --git a/web/pw-server/src/lib/components/RulesView.svelte b/web/pw-server/src/lib/components/RulesView.svelte index c3cf125..58e6bf2 100644 --- a/web/pw-server/src/lib/components/RulesView.svelte +++ b/web/pw-server/src/lib/components/RulesView.svelte @@ -64,7 +64,6 @@ padding: 15px; overflow-y: scroll; height: 100%; - margin-bottom: 200px; box-sizing: border-box; } </style> diff --git a/web/pw-server/src/lib/components/navbar/UserControls.svelte b/web/pw-server/src/lib/components/navbar/UserControls.svelte index 3c1d181..0b8413d 100644 --- a/web/pw-server/src/lib/components/navbar/UserControls.svelte +++ b/web/pw-server/src/lib/components/navbar/UserControls.svelte @@ -1,5 +1,5 @@ <script lang="ts"> - import { get_session_token } from "$lib/auth"; + import { get_session_token, clear_session_token } from "$lib/auth"; import { currentUser } from "$lib/stores/current_user"; import { onMount } from "svelte"; @@ -30,6 +30,7 @@ function signOut() { // TODO: destroy session on server currentUser.set(null); + clear_session_token(); } </script> diff --git a/web/pw-server/src/lib/utils.ts b/web/pw-server/src/lib/utils.ts index aab9734..155d952 100644 --- a/web/pw-server/src/lib/utils.ts +++ b/web/pw-server/src/lib/utils.ts @@ -1,3 +1,5 @@ +import { get_session_token } from "./auth"; + export function debounce(func: Function, timeout: number = 300) { let timer: ReturnType<typeof setTimeout>; return (...args: any[]) => { @@ -7,3 +9,36 @@ export function debounce(func: Function, timeout: number = 300) { }, timeout); }; } + +export async function get(url: string, fetch_fn: Function = fetch) { + const headers = { "Content-Type": "application/json" }; + + const token = get_session_token(); + if (token) { + headers["Authorization"] = `Bearer ${token}`; + } + + const response = await fetch_fn(url, { + method: "GET", + headers, + }); + + return JSON.parse(response); +} + +export async function post(url: string, data: any, fetch_fn: Function = fetch) { + const headers = { "Content-Type": "application/json" }; + + const token = get_session_token(); + if (token) { + headers["Authorization"] = `Bearer ${token}`; + } + + const response = await fetch_fn(url, { + method: "POST", + headers, + body: JSON.stringify(data), + }); + + return JSON.parse(response); +} |