+layout.svelte 1.16 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
21
22
		if (backendConfig) {
			await config.set(backendConfig);
			console.log(backendConfig);
23

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

32
33
					if (sessionUser) {
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
34
35
36
37
					} else {
						localStorage.removeItem('token');
						await goto('/auth');
					}
38
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
39
					await goto('/auth');
40
41
				}
			}
42
43
		} else {
			await goto(`/error`);
44
45
46
47
48
		}

		await tick();
		loaded = true;
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
49
50
51
52
53
</script>

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

55
{#if loaded}
56
57
	<slot />
{/if}
58
59

<Toaster />