Skip to content

Commit 74caa55

Browse files
committed
Revert alot of changes
1 parent 88c637a commit 74caa55

17 files changed

Lines changed: 185 additions & 179 deletions

File tree

src/lib/init.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { browser } from '$app/environment';
2+
import { initializeDarkModeStore } from '$lib/stores/ColorSchemeStore';
3+
import { initializeFlashManagersStore } from '$lib/stores/FlashManagersStore';
4+
import { initializeSerialPortsStore } from '$lib/stores/SerialPortsStore';
5+
import { UserStore } from '$lib/stores/UserStore';
6+
import { get, writable } from 'svelte/store';
7+
import { destroySignalR, initializeSignalR } from './signalr';
8+
9+
const isInitializedStore = writable<true | undefined>();
10+
11+
export async function initializeApp(initializeAuth: boolean) {
12+
if (!browser) return;
13+
14+
// Multiple init protection
15+
if (get(isInitializedStore)) return;
16+
isInitializedStore.set(true);
17+
18+
initializeDarkModeStore();
19+
initializeFlashManagersStore(); // TODO: Move this OUT of here
20+
initializeSerialPortsStore(); // TODO: Move this OUT of here
21+
22+
if (!initializeAuth) return;
23+
24+
const isAuthenticated = get(UserStore).self !== null;
25+
if (!isAuthenticated) {
26+
// Try to authenticate
27+
if (!(await UserStore.refreshSelf())) {
28+
// Failed to authenticate
29+
return;
30+
}
31+
}
32+
33+
// Initialize SignalR connection
34+
await initializeSignalR();
35+
36+
return true;
37+
}
38+
39+
export async function destroyAuth() {
40+
UserStore.reset();
41+
await destroySignalR();
42+
}

src/lib/server/cookie.ts

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

src/lib/signalr/index.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import {
55
HubConnectionState,
66
LogLevel,
77
} from '@microsoft/signalr';
8-
import { browser, dev } from '$app/environment';
8+
import { dev } from '$app/environment';
99
import { PUBLIC_BACKEND_API_DOMAIN } from '$env/static/public';
10-
import { UserStore } from '$lib/stores/UserStore';
1110
import { toast } from 'svelte-sonner';
1211
import { type Readable, get, writable } from 'svelte/store';
1312
import { handleSignalrDeviceState } from './handlers/DeviceStatus';
@@ -21,7 +20,7 @@ import {
2120
const signalr_connection = writable<HubConnection | null>(null);
2221
const signalr_state = writable<HubConnectionState>(HubConnectionState.Disconnected);
2322

24-
async function create_signalr_connection() {
23+
export async function initializeSignalR() {
2524
let connection = get(signalr_connection);
2625
if (connection) {
2726
return;
@@ -62,16 +61,30 @@ async function create_signalr_connection() {
6261

6362
signalr_connection.set(connection);
6463

65-
await connection.start();
64+
try {
65+
await connection.start();
66+
signalr_state.set(HubConnectionState.Connected);
67+
} catch (error) {
68+
console.error(error);
69+
toast.error('Failed to connect to server!');
70+
signalr_state.set(HubConnectionState.Disconnected);
71+
}
6672
}
6773

68-
function destroy_signalr_connection() {
69-
if (signalr_connection) {
74+
export async function destroySignalR() {
75+
if (!signalr_connection) return;
76+
77+
try {
7078
const connection = get(signalr_connection);
7179
if (connection) {
72-
connection.stop();
73-
signalr_connection.set(null);
80+
await connection.stop();
7481
}
82+
} catch (error) {
83+
console.error(error);
84+
toast.error('Encountered error while disconnecting from server!');
85+
} finally {
86+
signalr_connection.set(null);
87+
signalr_state.set(HubConnectionState.Disconnected);
7588
}
7689
}
7790

@@ -82,23 +95,3 @@ export const SignalR_State = {
8295
export const SignalR_Connection = {
8396
subscribe: signalr_connection.subscribe,
8497
} as Readable<HubConnection | null>;
85-
86-
export function initializeSignalR() {
87-
if (!browser) return;
88-
89-
UserStore.subscribe(({ self }) => {
90-
if (self === null) {
91-
destroy_signalr_connection();
92-
} else {
93-
create_signalr_connection()
94-
.then(() => {
95-
signalr_state.set(HubConnectionState.Connected);
96-
})
97-
.catch((e) => {
98-
console.error(e);
99-
toast.error('Failed to connect to server!');
100-
signalr_state.set(HubConnectionState.Disconnected);
101-
});
102-
}
103-
});
104-
}

src/lib/stores/AuthenticatedStore.ts

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

src/lib/stores/ColorSchemeStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export function willActivateLightMode(value: 'dark' | 'light' | 'system') {
100100
export function initializeDarkModeStore() {
101101
const schemePreference = getColorSchemePreference();
102102
setHtmlDarkModeSelector(schemePreference === 'dark');
103-
set(schemePreference);
104103

105104
window
106105
.matchMedia('(prefers-color-scheme: light)')

src/lib/stores/UserStore.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { usersApi } from '$lib/api';
2+
import { handleApiError } from '$lib/errorhandling/apiErrorHandling';
23
import type { ApiUser, ApiUserSelf } from '$lib/types/ApiUser';
34
import { writable } from 'svelte/store';
45

@@ -35,35 +36,40 @@ function setSelfName(name: string) {
3536
});
3637
}
3738

38-
function refreshSelf() {
39+
async function refreshSelf(): Promise<boolean> {
3940
update((v) => ({ ...v, loading: true }));
40-
usersApi
41-
.usersGetSelf()
42-
.then(({ data, message }) => {
43-
if (!data) {
44-
console.error(`Failed to get user self: ${message}`);
45-
reset();
46-
return;
47-
}
4841

49-
const user = {
50-
id: data.id,
51-
name: data.name,
52-
avatar: data.image,
53-
roles: data.roles,
54-
email: data.email,
55-
};
42+
try {
43+
const { data, message } = await usersApi.usersGetSelf();
5644

57-
update((state) => ({
58-
loading: false,
59-
self: user,
60-
all: updateAllFromSelf(state.all, user),
61-
}));
62-
})
63-
.catch((error) => {
64-
update((v) => ({ ...v, loading: false }));
65-
console.error(error); // TODO: Show toast
66-
});
45+
if (!data) {
46+
console.error(`Failed to get user self: ${message}`);
47+
reset();
48+
return false;
49+
}
50+
51+
const user = {
52+
id: data.id,
53+
name: data.name,
54+
avatar: data.image,
55+
roles: data.roles,
56+
email: data.email,
57+
};
58+
59+
update((state) => ({
60+
loading: false,
61+
self: user,
62+
all: updateAllFromSelf(state.all, user),
63+
}));
64+
} catch (error) {
65+
reset();
66+
67+
handleApiError(error);
68+
69+
return false;
70+
}
71+
72+
return true;
6773
}
6874

6975
export const UserStore = {
@@ -74,7 +80,3 @@ export const UserStore = {
7480
refreshSelf,
7581
reset,
7682
};
77-
78-
export function initializeUserStore() {
79-
refreshSelf();
80-
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { redirect } from '@sveltejs/kit';
2+
3+
export async function load({ cookies, url }) {
4+
const cookieValue = cookies.get('openShockSession');
5+
if (cookieValue !== undefined && cookieValue !== null) {
6+
redirect(302, url.searchParams.get('redirect') ?? '/home');
7+
}
8+
}

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1 @@
1-
import { browser } from '$app/environment';
2-
import { IsAuthenticated } from '$lib/stores/AuthenticatedStore';
3-
import { UserStore } from '$lib/stores/UserStore';
4-
import { redirect } from '@sveltejs/kit';
5-
import { get } from 'svelte/store';
6-
7-
// Makes the load function only run in browser
8-
export const ssr = false;
9-
10-
// Its fine to prerender all pages below this one as they wont change from browser to browser
111
export const prerender = true;
12-
13-
export function load() {
14-
if (!browser) return; // Just to be safe that the code below definetly runs in browser
15-
16-
console.log('(anonymous)/+layout.ts (browser) - entry');
17-
18-
// On loading in the anonymous section, check if cookie is set, if it is send us to the authenticated section
19-
if (get(IsAuthenticated)) {
20-
console.log('(anonymous)/+layout.ts (browser) - redirect');
21-
redirect(302, '/home');
22-
}
23-
24-
// If we make it to the anonymous section, make sure that userstore is clear, else behaviour will look weird
25-
UserStore.reset();
26-
}

src/routes/(anonymous)/login/+page.svelte

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { goto } from '$app/navigation';
3+
import { page } from '$app/state';
34
import { accountV2Api } from '$lib/api';
45
import Container from '$lib/components/Container.svelte';
56
import Turnstile from '$lib/components/Turnstile.svelte';
@@ -14,20 +15,20 @@
1415
let password = $state<string>('');
1516
let turnstileResponse = $state<string | null>(null);
1617
17-
function handleSubmission(e: SubmitEvent) {
18+
async function handleSubmission(e: SubmitEvent) {
1819
e.preventDefault();
1920
2021
if (!usernameOrEmail || !password || !turnstileResponse) {
2122
return;
2223
}
2324
24-
accountV2Api
25-
.accountLoginV2({ usernameOrEmail, password, turnstileResponse })
26-
.then(() => {
27-
UserStore.refreshSelf();
28-
goto('/home');
29-
})
30-
.catch(handleApiError);
25+
try {
26+
await accountV2Api.accountLoginV2({ usernameOrEmail, password, turnstileResponse });
27+
await UserStore.refreshSelf();
28+
goto(page.url.searchParams.get('redirect') ?? '/home');
29+
} catch (error) {
30+
handleApiError(error);
31+
}
3132
}
3233
3334
let canSubmit = $derived(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { browser } from '$app/environment';
2+
import { goto } from '$app/navigation';
3+
import { accountV1Api } from '$lib/api';
4+
import { destroyAuth } from '$lib/init';
5+
6+
export async function load() {
7+
if (!browser) return; // Do not run the following on server
8+
9+
// Clear cookie and server state
10+
try {
11+
await accountV1Api.accountLogout();
12+
} catch (error) {
13+
console.error(error);
14+
}
15+
16+
try {
17+
// Clear local context
18+
await destroyAuth();
19+
} catch (error) {
20+
console.error(error);
21+
}
22+
23+
// Go to landing page
24+
goto('/');
25+
}

0 commit comments

Comments
 (0)