|
1 | 1 | <script lang="ts"> |
2 | 2 | import type { Snippet } from 'svelte'; |
3 | 3 | import { onMount } from 'svelte'; |
| 4 | + import { goto } from '$app/navigation'; |
4 | 5 | import { page } from '$app/stores'; |
5 | 6 | import '../app.css'; |
6 | 7 | import favicon from '$lib/assets/favicon.svg'; |
|
20 | 21 |
|
21 | 22 | let { children, toolbar }: { children?: Snippet; toolbar?: Snippet } = $props(); |
22 | 23 |
|
| 24 | + // Client-side auth guard |
| 25 | + $effect(() => { |
| 26 | + if (authStore.loading || authStore.authEnabled === null) return; |
| 27 | + if (authStore.authEnabled === false) return; |
| 28 | +
|
| 29 | + const path = $page.url.pathname; |
| 30 | +
|
| 31 | + if (path === '/login') { |
| 32 | + if (authStore.isAuthenticated && authStore.isApproved) { |
| 33 | + goto('/', { replaceState: true }); |
| 34 | + } |
| 35 | + } else if (path === '/pending-approval') { |
| 36 | + if (!authStore.isAuthenticated) { |
| 37 | + goto('/login', { replaceState: true }); |
| 38 | + } else if (authStore.isApproved) { |
| 39 | + goto('/', { replaceState: true }); |
| 40 | + } |
| 41 | + } else { |
| 42 | + if (!authStore.isAuthenticated) { |
| 43 | + goto('/login', { replaceState: true }); |
| 44 | + } else if (authStore.isPending) { |
| 45 | + goto('/pending-approval', { replaceState: true }); |
| 46 | + } else if (path.startsWith('/admin') && !authStore.isAdmin) { |
| 47 | + goto('/', { replaceState: true }); |
| 48 | + } |
| 49 | + } |
| 50 | + }); |
| 51 | +
|
23 | 52 | const pageInfo = $derived.by(() => { |
24 | 53 | const path = $page.url.pathname; |
25 | 54 | if (path === '/') return { name: 'Home', url: '/' }; |
|
62 | 91 | <ModeWatcher /> |
63 | 92 | <Toaster position="bottom-right" closeButton richColors duration={8000} /> |
64 | 93 |
|
65 | | -{#if showSidebar} |
| 94 | +{#if authStore.loading} |
| 95 | + <div class="flex min-h-svh items-center justify-center"> |
| 96 | + <div class="h-8 w-8 animate-spin rounded-full border-4 border-muted border-t-foreground"></div> |
| 97 | + </div> |
| 98 | +{:else if showSidebar} |
66 | 99 | <Sidebar.Provider bind:open={sidebarStore.open}> |
67 | 100 | <AppSidebar /> |
68 | 101 | <Sidebar.Inset> |
|
0 commit comments