Skip to content

Commit de80fad

Browse files
committed
Fix navigation spamming API
1 parent 942872b commit de80fad

15 files changed

Lines changed: 56 additions & 60 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { writable } from 'svelte/store';
2+
3+
export const IsAuthenticated = writable<boolean | undefined>();

src/lib/stores/index.ts

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

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

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { browser } from '$app/environment';
2+
import { goto } from '$app/navigation';
3+
import { IsAuthenticated } from '$lib/stores/AuthenticatedStore';
24
import { UserStore } from '$lib/stores/UserStore';
5+
import { get } from 'svelte/store';
36

47
// Makes the load function only run in browser
58
export const ssr = false;
@@ -10,6 +13,11 @@ export const prerender = true;
1013
export function load() {
1114
if (!browser) return; // Just to be safe that the code below definetly runs in browser
1215

16+
// On loading in the anonymous section, check if cookie is set, if it is send us to the authenticated section
17+
if (get(IsAuthenticated)) {
18+
goto('/home');
19+
}
20+
1321
// If we make it to the anonymous section, make sure that userstore is clear, else behaviour will look weird
1422
UserStore.reset();
1523
}

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

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { browser } from '$app/environment';
2-
import { initializeSignalR } from '$lib/signalr';
3-
import { initializeStores } from '$lib/stores';
2+
import { goto } from '$app/navigation';
3+
import { IsAuthenticated } from '$lib/stores/AuthenticatedStore';
4+
import { get } from 'svelte/store';
45

56
// The pages below this one will be different from user-to-user so cannot be prerendered and really shouldnt be server rendered
6-
export const ssr = false;
7+
export const ssr = false; // Only render authenticated pages in browser
78
export const prerender = false;
89

910
// Initialize stores and signalr on auth
10-
export function load() {
11-
if (!browser) return; // Yeah please dont run the context init in server lmao
11+
export function load({ url }) {
12+
if (!browser) return; // Yeah please dont run on server
1213

13-
initializeStores();
14-
initializeSignalR();
14+
// On loading in the anonymous section, check if cookie is set, if it is send us to the authenticated section
15+
if (!get(IsAuthenticated)) {
16+
goto(`/login?redirect=${url.pathname}`);
17+
}
1518
};

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

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HasOpenShockCookie } from '$lib/server/cookie';
22

3-
// Report to the browser if we are authenticated or not
3+
// Let the client know if its authenticated or not
44
export function load({ cookies }) {
55
return {
66
isAuthenticated: HasOpenShockCookie(cookies)

src/routes/+layout.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
import { browser } from '$app/environment';
2+
import { initializeSignalR } from '$lib/signalr';
3+
import { IsAuthenticated } from '$lib/stores/AuthenticatedStore';
4+
import { initializeDarkModeStore } from '$lib/stores/ColorSchemeStore.js';
5+
import { initializeFlashManagersStore } from '$lib/stores/FlashManagersStore.js';
6+
import { initializeSerialPortsStore } from '$lib/stores/SerialPortsStore.js';
7+
import { initializeUserStore } from '$lib/stores/UserStore.js';
8+
import { get } from 'svelte/store';
9+
110
// Set the default for the application
2-
export const ssr = false;
3-
export const prerender = false;
11+
export const ssr = false; // Only this file and pages under it in browser
12+
export const prerender = true;
13+
14+
export function load({ data }) {
15+
if (!browser) return; // Be completely sure this only runs in browser
16+
17+
const wasAuthenticated = get(IsAuthenticated);
18+
if (wasAuthenticated !== data.isAuthenticated) {
19+
// Set authentication state
20+
IsAuthenticated.set(data.isAuthenticated);
21+
22+
// First time init
23+
if (wasAuthenticated === undefined) {
24+
initializeDarkModeStore();
25+
initializeFlashManagersStore();
26+
initializeSerialPortsStore();
27+
}
28+
29+
// Init on getting authenticated
30+
if (data.isAuthenticated) {
31+
initializeUserStore();
32+
initializeSignalR();
33+
}
34+
}
35+
}
File renamed without changes.

0 commit comments

Comments
 (0)