From 87c35107ce01bfd380697ad8b93b10cbda359416 Mon Sep 17 00:00:00 2001 From: EETagent Date: Thu, 12 Jan 2023 13:19:17 +0100 Subject: [PATCH] feat: add auto logout --- .../admin/(authenticated)/+layout.server.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/frontend/src/routes/(admin)/admin/(authenticated)/+layout.server.ts b/frontend/src/routes/(admin)/admin/(authenticated)/+layout.server.ts index af8cb9a..55d325c 100644 --- a/frontend/src/routes/(admin)/admin/(authenticated)/+layout.server.ts +++ b/frontend/src/routes/(admin)/admin/(authenticated)/+layout.server.ts @@ -1,10 +1,19 @@ import type { LayoutServerLoad } from './$types'; import { redirect } from '@sveltejs/kit'; +import { apiWhoami } from '$lib/@api/admin'; -export const load: LayoutServerLoad = ({ cookies }) => { +export const load: LayoutServerLoad = async ({ cookies, fetch }) => { const isAuthenticated = cookies.get('id'); - if (!isAuthenticated) { - throw redirect(302, '/admin/auth/login'); + + if (isAuthenticated) { + const whoami = await apiWhoami(fetch).catch((e) => { + throw redirect(302, '/admin/auth/logout'); + }); + return { + whoami: whoami + }; + } else { + throw redirect(302, '/admin/auth/logout'); } };