diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts new file mode 100644 index 0000000..97707b9 --- /dev/null +++ b/src/routes/+layout.ts @@ -0,0 +1,13 @@ +import type { LayoutLoad } from './$types'; + +export const load: LayoutLoad = async ({ fetch }) => { + const user = await fetch('/api/public/1/auth/myself/', { + method: 'GET', + headers: { + 'Accept': 'application/json', + } + }); + return { + user: user.ok ? await user.json() : null + }; +}; diff --git a/src/routes/[qrcode]/+layout.svelte b/src/routes/[qrcode]/+layout.svelte index d347d7a..fb84683 100644 --- a/src/routes/[qrcode]/+layout.svelte +++ b/src/routes/[qrcode]/+layout.svelte @@ -3,10 +3,32 @@ import type { LayoutProps } from './$types'; import ConnType2 from "$lib/icons/ConnType2.svelte"; import Icon from "@iconify/svelte"; + import { goto, invalidateAll } from "$app/navigation"; let { data, children }: LayoutProps = $props(); const cc = $derived(data.chargecontroller); + const user = $derived(data.user); const qrcode = $derived(page.params.qrcode); + + let logouting = $state(false); + + async function logout() { + logouting = true; + try { + const response = await fetch("/api/public/1/auth/logout/", { + method: 'GET', + headers: { + 'Accept': 'application/json', + } + }); + if (response.ok) { + await invalidateAll(); + await goto(`/${qrcode}`); + } + } finally { + logouting = false; + } + }