From a893be89c5e2863ae44af3fa2c2639604ced1278 Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Thu, 29 Sep 2022 18:05:48 +0800
Subject: refactor: Check session and redirect server side

---
 apps/kit/src/routes/(main)/+layout.svelte | 29 +++--------------------------
 1 file changed, 3 insertions(+), 26 deletions(-)

(limited to 'apps/kit/src/routes/(main)/+layout.svelte')

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 />
-- 
cgit v1.3

