+layout.svelte 1.33 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script>
2
3
4
5
	import { onMount, tick } from 'svelte';
	import { config, user } from '$lib/stores';
	import { goto } from '$app/navigation';
	import toast, { Toaster } from 'svelte-french-toast';
Timothy J. Baek's avatar
Timothy J. Baek committed
6

Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
9
	import { getBackendConfig } from '$lib/apis';
	import { getSessionUser } from '$lib/apis/auths';

Timothy J. Baek's avatar
Timothy J. Baek committed
10
11
	import '../app.css';
	import '../tailwind.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
12
	import 'tippy.js/dist/tippy.css';
Timothy J. Baek's avatar
Timothy J. Baek committed
13

14
15
16
	let loaded = false;

	onMount(async () => {
17
		// Check Backend Status
Timothy J. Baek's avatar
Timothy J. Baek committed
18
		const backendConfig = await getBackendConfig();
19

Timothy J. Baek's avatar
Timothy J. Baek committed
20
		if (backendConfig) {
Timothy J. Baek's avatar
Timothy J. Baek committed
21
			// Save Backend Status to Store
Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
			await config.set(backendConfig);
			console.log(backendConfig);
24

25
			if ($config) {
26
				if (localStorage.token) {
27
					// Get Session User Info
Timothy J. Baek's avatar
Timothy J. Baek committed
28
29
30
31
					const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
						toast.error(error);
						return null;
					});
32

33
					if (sessionUser) {
Timothy J. Baek's avatar
Timothy J. Baek committed
34
						// Save Session User to Store
35
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
36
					} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
37
						// Redirect Invalid Session User to /auth Page
Timothy J. Baek's avatar
Timothy J. Baek committed
38
39
40
						localStorage.removeItem('token');
						await goto('/auth');
					}
41
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
42
					await goto('/auth');
43
44
				}
			}
45
		} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
46
			// Redirect to /error when Backend Not Detected
47
			await goto(`/error`);
48
49
50
51
52
		}

		await tick();
		loaded = true;
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
53
54
55
56
57
</script>

<svelte:head>
	<title>Ollama</title>
</svelte:head>
58

59
{#if loaded}
60
61
	<slot />
{/if}
62
63

<Toaster />