|
1 | 1 | import { browser } from '$app/environment'; |
| 2 | +import { goto } from '$app/navigation'; |
| 3 | +import { accountV1Api } from '$lib/api/index.js'; |
2 | 4 | import { initializeSignalR } from '$lib/signalr'; |
3 | 5 | import { IsAuthenticated } from '$lib/stores/AuthenticatedStore'; |
4 | 6 | import { initializeDarkModeStore } from '$lib/stores/ColorSchemeStore.js'; |
5 | 7 | import { initializeFlashManagersStore } from '$lib/stores/FlashManagersStore.js'; |
6 | 8 | import { initializeSerialPortsStore } from '$lib/stores/SerialPortsStore.js'; |
7 | | -import { initializeUserStore } from '$lib/stores/UserStore.js'; |
| 9 | +import { initializeUserStore, UserStore } from '$lib/stores/UserStore.js'; |
8 | 10 | import { get } from 'svelte/store'; |
9 | 11 |
|
10 | 12 | // Set the default for the application |
11 | 13 | export const ssr = false; // Only this file and pages under it in browser |
12 | 14 | export const prerender = true; |
13 | 15 |
|
14 | | -export function load({ data }) { |
| 16 | +export async function load({ data, url }) { |
15 | 17 | if (!browser) return; // Be completely sure this only runs in browser |
16 | 18 |
|
| 19 | + console.log('+layout.ts (browser) - entry'); |
| 20 | + |
| 21 | + if (url.pathname === '/logout') { |
| 22 | + console.log('+layout.ts (browser) - logout'); |
| 23 | + |
| 24 | + // Clear local store |
| 25 | + UserStore.reset(); |
| 26 | + |
| 27 | + // Clear cookie and server state |
| 28 | + try { |
| 29 | + await accountV1Api.accountLogout(); |
| 30 | + } catch { |
| 31 | + // Do nothing |
| 32 | + } |
| 33 | + |
| 34 | + // Go to landing page |
| 35 | + goto('/'); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
17 | 39 | const wasAuthenticated = get(IsAuthenticated); |
18 | 40 | if (wasAuthenticated !== data.isAuthenticated) { |
| 41 | + console.log('+layout.ts (browser) - initialize'); |
19 | 42 | // Set authentication state |
20 | 43 | IsAuthenticated.set(data.isAuthenticated); |
21 | 44 |
|
22 | 45 | // First time init |
23 | 46 | if (wasAuthenticated === undefined) { |
| 47 | + console.log('+layout.ts (browser) - initialize first time'); |
24 | 48 | initializeDarkModeStore(); |
25 | 49 | initializeFlashManagersStore(); |
26 | 50 | initializeSerialPortsStore(); |
27 | 51 | } |
28 | 52 |
|
29 | 53 | // Init on getting authenticated |
30 | 54 | if (data.isAuthenticated) { |
| 55 | + console.log('+layout.ts (browser) - initialize auth'); |
31 | 56 | initializeUserStore(); |
32 | 57 | initializeSignalR(); |
33 | 58 | } |
|
0 commit comments