Skip to content

Commit 4036d74

Browse files
committed
Welp, try another fix
1 parent de80fad commit 4036d74

4 files changed

Lines changed: 33 additions & 23 deletions

File tree

src/routes/(anonymous)/+layout.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ export const prerender = true;
1313
export function load() {
1414
if (!browser) return; // Just to be safe that the code below definetly runs in browser
1515

16+
console.log('(anonymous)/+layout.ts (browser) - entry');
17+
1618
// On loading in the anonymous section, check if cookie is set, if it is send us to the authenticated section
1719
if (get(IsAuthenticated)) {
20+
console.log('(anonymous)/+layout.ts (browser) - redirect');
1821
goto('/home');
1922
}
2023

src/routes/(authenticated)/+layout.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ export const prerender = false;
1111
export function load({ url }) {
1212
if (!browser) return; // Yeah please dont run on server
1313

14+
console.log('(anonymous)/+layout.ts (browser) - entry');
15+
1416
// On loading in the anonymous section, check if cookie is set, if it is send us to the authenticated section
1517
if (!get(IsAuthenticated)) {
18+
console.log('(anonymous)/+layout.ts (browser) - redirect');
1619
goto(`/login?redirect=${url.pathname}`);
1720
}
1821
};

src/routes/+layout.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
11
import { browser } from '$app/environment';
2+
import { goto } from '$app/navigation';
3+
import { accountV1Api } from '$lib/api/index.js';
24
import { initializeSignalR } from '$lib/signalr';
35
import { IsAuthenticated } from '$lib/stores/AuthenticatedStore';
46
import { initializeDarkModeStore } from '$lib/stores/ColorSchemeStore.js';
57
import { initializeFlashManagersStore } from '$lib/stores/FlashManagersStore.js';
68
import { initializeSerialPortsStore } from '$lib/stores/SerialPortsStore.js';
7-
import { initializeUserStore } from '$lib/stores/UserStore.js';
9+
import { initializeUserStore, UserStore } from '$lib/stores/UserStore.js';
810
import { get } from 'svelte/store';
911

1012
// Set the default for the application
1113
export const ssr = false; // Only this file and pages under it in browser
1214
export const prerender = true;
1315

14-
export function load({ data }) {
16+
export async function load({ data, url }) {
1517
if (!browser) return; // Be completely sure this only runs in browser
1618

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+
1739
const wasAuthenticated = get(IsAuthenticated);
1840
if (wasAuthenticated !== data.isAuthenticated) {
41+
console.log('+layout.ts (browser) - initialize');
1942
// Set authentication state
2043
IsAuthenticated.set(data.isAuthenticated);
2144

2245
// First time init
2346
if (wasAuthenticated === undefined) {
47+
console.log('+layout.ts (browser) - initialize first time');
2448
initializeDarkModeStore();
2549
initializeFlashManagersStore();
2650
initializeSerialPortsStore();
2751
}
2852

2953
// Init on getting authenticated
3054
if (data.isAuthenticated) {
55+
console.log('+layout.ts (browser) - initialize auth');
3156
initializeUserStore();
3257
initializeSignalR();
3358
}

src/routes/logout/+page.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)