From a893be89c5e2863ae44af3fa2c2639604ced1278 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Thu, 29 Sep 2022 18:05:48 +0800 Subject: refactor: Check session and redirect server side --- apps/kit/src/lib/configuration.ts | 3 +- apps/kit/src/routes/(main)/(public)/+layout.svelte | 1 - apps/kit/src/routes/(main)/+layout.server.ts | 32 +++++++++++++--------- apps/kit/src/routes/(main)/+layout.svelte | 29 ++------------------ apps/kit/src/routes/(main)/+page.svelte | 1 - 5 files changed, 24 insertions(+), 42 deletions(-) delete mode 100644 apps/kit/src/routes/(main)/(public)/+layout.svelte diff --git a/apps/kit/src/lib/configuration.ts b/apps/kit/src/lib/configuration.ts index d6f6b4f..609a00f 100644 --- a/apps/kit/src/lib/configuration.ts +++ b/apps/kit/src/lib/configuration.ts @@ -24,7 +24,8 @@ export function is_debug(): boolean { export const CookieNames = { theme: "go_theme", - locale: "go_locale" + locale: "go_locale", + session: "go_session" }; export const QueryKeys = { diff --git a/apps/kit/src/routes/(main)/(public)/+layout.svelte b/apps/kit/src/routes/(main)/(public)/+layout.svelte deleted file mode 100644 index 49aeb95..0000000 --- a/apps/kit/src/routes/(main)/(public)/+layout.svelte +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/apps/kit/src/routes/(main)/+layout.server.ts b/apps/kit/src/routes/(main)/+layout.server.ts index 01aae89..32b0583 100644 --- a/apps/kit/src/routes/(main)/+layout.server.ts +++ b/apps/kit/src/routes/(main)/+layout.server.ts @@ -1,13 +1,19 @@ -// import {is_active} from "$lib/session"; -// import {redirect} from "@sveltejs/kit"; -// import type {LayoutServerLoad} from "./$types"; -// -// export const load: LayoutServerLoad = async ({routeId}) => { -// const sessionIsValid = await is_active(); -// const isPublicRoute = routeId?.startsWith("(public)"); -// if (sessionIsValid && isPublicRoute) { -// throw redirect(302, "/home"); -// } else if (!sessionIsValid && !isPublicRoute) { -// throw redirect(302, "/login"); -// } -// }; \ No newline at end of file +import { api_base, CookieNames } from "$lib/configuration"; +import { redirect } from "@sveltejs/kit"; +import type { LayoutServerLoad } from "./$types"; + +export const load: LayoutServerLoad = async ({ routeId, cookies }) => { + const isPublicRoute = routeId?.startsWith("(main)/(public)") ?? true; + const sessionCookie = cookies.get(CookieNames.session); + const sessionIsValid = (await fetch(api_base("_/valid-session"), { + headers: { + Cookie: CookieNames.session + "=" + sessionCookie, + } + })).ok + + if (sessionIsValid && isPublicRoute) { + throw redirect(302, "/home"); + } else if (!sessionIsValid && !isPublicRoute) { + throw redirect(302, "/sign-in"); + } +}; \ No newline at end of file diff --git a/apps/kit/src/routes/(main)/+layout.svelte b/apps/kit/src/routes/(main)/+layout.svelte index ec50c4b..3107861 100644 --- a/apps/kit/src/routes/(main)/+layout.svelte +++ b/apps/kit/src/routes/(main)/+layout.svelte @@ -1,38 +1,15 @@ diff --git a/apps/kit/src/routes/(main)/+page.svelte b/apps/kit/src/routes/(main)/+page.svelte index 85a4d2d..e507a19 100644 --- a/apps/kit/src/routes/(main)/+page.svelte +++ b/apps/kit/src/routes/(main)/+page.svelte @@ -1,2 +1 @@ -

Hold on...

-- cgit v1.3