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; + } + }
@@ -17,16 +39,29 @@
diff --git a/src/routes/[qrcode]/+layout.ts b/src/routes/[qrcode]/+layout.ts index fec4f43..a0c050c 100644 --- a/src/routes/[qrcode]/+layout.ts +++ b/src/routes/[qrcode]/+layout.ts @@ -4,16 +4,19 @@ import { type } from "arktype"; const QrcodeType = type("string.integer.parse"); -export const load: LayoutLoad = async ({ params, fetch }) => { +export const load: LayoutLoad = async ({ params, fetch, parent }) => { const qrcode = QrcodeType(params.qrcode); if (qrcode instanceof type.errors) error(400, 'invalid qrcode'); - const data = await fetch(`/api/v2/chargecontroller/${qrcode}/`, { + const cc = await fetch(`/api/v2/chargecontroller/${qrcode}/`, { method: 'GET', headers: { 'Accept': 'application/json', } }); + const parentData = await parent(); return { - chargecontroller: await data.json(), + ...parentData, + qrcode: qrcode, + chargecontroller: await cc.json(), }; }; \ No newline at end of file diff --git a/src/routes/[qrcode]/+page.svelte b/src/routes/[qrcode]/+page.svelte index 55e5cd2..996ce19 100644 --- a/src/routes/[qrcode]/+page.svelte +++ b/src/routes/[qrcode]/+page.svelte @@ -1,3 +1,3 @@
- TEST CARD + START CARD
\ No newline at end of file diff --git a/src/routes/[qrcode]/+page.ts b/src/routes/[qrcode]/+page.ts new file mode 100644 index 0000000..e485217 --- /dev/null +++ b/src/routes/[qrcode]/+page.ts @@ -0,0 +1,8 @@ +import type { PageLoad } from './$types'; +import { redirect } from "@sveltejs/kit"; + +export const load: PageLoad = async ({ parent }) => { + const parentData = await parent(); + if (parentData.user === null) return redirect(303, `${parentData.qrcode}/login`); + return parentData; +}; diff --git a/src/routes/[qrcode]/login/+page.svelte b/src/routes/[qrcode]/login/+page.svelte new file mode 100644 index 0000000..39c5a8d --- /dev/null +++ b/src/routes/[qrcode]/login/+page.svelte @@ -0,0 +1,89 @@ + + +
+
+
+

Accesso Utente

+
+ + +
+
+ +
+ {#if $message} +
+
+

+ {$message['status'] === 'success' ? 'Success!' : 'Error'} +

+
+ {$message['text']} +
+
+
+ {/if} +
+
+
\ No newline at end of file diff --git a/src/routes/[qrcode]/login/+page.ts b/src/routes/[qrcode]/login/+page.ts new file mode 100644 index 0000000..138b367 --- /dev/null +++ b/src/routes/[qrcode]/login/+page.ts @@ -0,0 +1,8 @@ +import type { PageLoad } from './$types'; +import { redirect } from "@sveltejs/kit"; + +export const load: PageLoad = async ({ parent }) => { + const parentData = await parent(); + if (parentData.user !== null) return redirect(303, `/${parentData.qrcode}`); + return parentData; +}; \ No newline at end of file diff --git a/src/routes/[qrcode]/status/+page.svelte b/src/routes/[qrcode]/status/+page.svelte new file mode 100644 index 0000000..0faa8f9 --- /dev/null +++ b/src/routes/[qrcode]/status/+page.svelte @@ -0,0 +1,3 @@ +
+ STATUS CARD +
\ No newline at end of file diff --git a/src/routes/[qrcode]/status/+page.ts b/src/routes/[qrcode]/status/+page.ts new file mode 100644 index 0000000..e485217 --- /dev/null +++ b/src/routes/[qrcode]/status/+page.ts @@ -0,0 +1,8 @@ +import type { PageLoad } from './$types'; +import { redirect } from "@sveltejs/kit"; + +export const load: PageLoad = async ({ parent }) => { + const parentData = await parent(); + if (parentData.user === null) return redirect(303, `${parentData.qrcode}/login`); + return parentData; +};