aboutsummaryrefslogtreecommitdiffstats
path: root/apps/kit/src/routes/(main)
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-09-29 12:05:48 +0200
committerivarlovlie <git@ivarlovlie.no>2022-09-29 12:06:18 +0200
commita893be89c5e2863ae44af3fa2c2639604ced1278 (patch)
treee54f870dc0b4f48884e12e2120cd632602670ccf /apps/kit/src/routes/(main)
parent7d69290660cf78e96b34483ea066f015af7fe1d9 (diff)
downloadgreatoffice-a893be89c5e2863ae44af3fa2c2639604ced1278.tar.xz
greatoffice-a893be89c5e2863ae44af3fa2c2639604ced1278.zip
refactor: Check session and redirect server side
Diffstat (limited to 'apps/kit/src/routes/(main)')
-rw-r--r--apps/kit/src/routes/(main)/(public)/+layout.svelte1
-rw-r--r--apps/kit/src/routes/(main)/+layout.server.ts32
-rw-r--r--apps/kit/src/routes/(main)/+layout.svelte29
-rw-r--r--apps/kit/src/routes/(main)/+page.svelte1
4 files changed, 22 insertions, 41 deletions
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 @@
-<slot></slot> \ 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 @@
<script lang="ts">
import "../../app.pcss";
- import { afterNavigate, beforeNavigate, goto } from "$app/navigation";
- import { is_active } from "$lib/session";
- import type { Navigation } from "@sveltejs/kit";
import { setLocale } from "$lib/i18n/i18n-svelte";
import { onMount } from "svelte";
import type { LayoutData } from "./$types";
import LocaleSwitcher from "$lib/components/locale-switcher.svelte";
export let data: LayoutData;
- onMount(() => setLocale(data.locale));
- async function redirect_if_necessary(ticket: Navigation) {
- const sessionIsValid = await is_active();
- // TODO: ticket.to can be empty while navigating, so coalesce could probably cause non-public routes to cause a redir to /login...
- const isPublicRoute = ticket.to?.routeId?.startsWith("(main)/(public)");
-
- console.log("redir: ", {
- isPublicRoute,
- sessionIsValid,
- });
-
- if (sessionIsValid && isPublicRoute) {
- await goto("/home");
- } else if (!sessionIsValid && !isPublicRoute) {
- await goto("/sign-in");
- }
- }
-
- // This should probably be removed in favor of the logic in layout.server.ts.
- // That requires a more sophisticated server side implementation of session handling,
- // and i don't want that tbh, i want to stay as much in the browser as possible.
- afterNavigate(redirect_if_necessary);
- beforeNavigate(redirect_if_necessary);
+ onMount(async () => {
+ setLocale(data.locale);
+ });
</script>
<LocaleSwitcher />
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 @@
-
<p class="text-bold p-1">Hold on...</p>