From 18c458d91ca5e7187ffb3615fca8970fc6e4ca65 Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Mon, 6 Jun 2022 15:48:53 +0200
Subject: feat: More work on portal

---
 apps/portal/src/app/index.svelte | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

(limited to 'apps/portal/src/app/index.svelte')

diff --git a/apps/portal/src/app/index.svelte b/apps/portal/src/app/index.svelte
index 0082aa2..fe143bc 100644
--- a/apps/portal/src/app/index.svelte
+++ b/apps/portal/src/app/index.svelte
@@ -2,32 +2,54 @@
 <svelte:window bind:online={online}/>
 
 <script>
-	import {projects_base} from "$shared/lib/configuration";
-	import Router from "svelte-spa-router";
+	import Router, {replace} from "svelte-spa-router";
 	import {wrap} from "svelte-spa-router/wrap";
 	import {is_active} from "$shared/lib/session";
 	import SignUp from "$app/pages/sign-up.svelte";
 	import Login from "$app/pages/login.svelte";
 	import Forgot from "$app/pages/forgot.svelte";
 	import Reset from "$app/pages/reset-password.svelte";
+	import Home from "$app/pages/home.svelte";
+	import ProfileHome from "$app/pages/profile/index.svelte";
 	import PreHeader from "$shared/components/pre-header.svelte";
 
 	let online = true;
 
-	async function user_is_logged_in() {
-		if (await is_active()) {
-			location.replace(projects_base("#/home"));
+	const publicRoutes = ["/login", "/signup", "/reset-password", "/forgot"];
+	const guardedRoutes = ["/", "/home", "/profile"];
+
+	async function user_is_logged_in(event) {
+		const isActive = await is_active();
+		if (!isActive && !publicRoutes.includes(event.route)) {
+			return false;
+		}
+		if (isActive && !guardedRoutes.includes(event.route)) {
+			await replace("/");
 		}
 		return true;
 	}
 
+	function route_guarded(event) {
+		if (!publicRoutes.includes(event.detail.route)) {
+			replace("/login");
+		}
+	}
+
 	const routes = {
 		"/login": wrap({
 			component: Login,
 			conditions: [user_is_logged_in],
 		}),
+		"/home": wrap({
+			component: Home,
+			conditions: [user_is_logged_in],
+		}),
+		"/profile": wrap({
+			component: ProfileHome,
+			conditions: [user_is_logged_in],
+		}),
 		"/": wrap({
-			component: Login,
+			component: Home,
 			conditions: [user_is_logged_in],
 		}),
 		"/signup": wrap({
@@ -50,6 +72,7 @@
 <Router
         {routes}
         restoreScrollState={true}
+        on:conditionsFailed={route_guarded}
         on:routeLoading={() => {
             document.getElementById("loader").style.display = "inline-block";
         }}
-- 
cgit v1.3

