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

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

	onMount(async () => {
14
15
		// Check Backend Status
		const res = await fetch(`${WEBUI_API_BASE_URL}/`, {
16
17
18
19
20
21
22
23
24
25
26
27
28
29
			method: 'GET',
			headers: {
				'Content-Type': 'application/json'
			}
		})
			.then(async (res) => {
				if (!res.ok) throw await res.json();
				return res.json();
			})
			.catch((error) => {
				console.log(error);
				return null;
			});

30
31
32
		if (res) {
			await config.set(res);
			console.log(res);
33

34
			if ($config) {
35
				if (localStorage.token) {
36
37
					// Get Session User Info
					const sessionUser = await fetch(`${WEBUI_API_BASE_URL}/auths`, {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
						method: 'GET',
						headers: {
							'Content-Type': 'application/json',
							Authorization: `Bearer ${localStorage.token}`
						}
					})
						.then(async (res) => {
							if (!res.ok) throw await res.json();
							return res.json();
						})
						.catch((error) => {
							console.log(error);
							toast.error(error.detail);
							return null;
						});

54
55
					if (sessionUser) {
						await user.set(sessionUser);
Timothy J. Baek's avatar
Timothy J. Baek committed
56
57
58
59
					} else {
						localStorage.removeItem('token');
						await goto('/auth');
					}
60
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
61
					await goto('/auth');
62
63
				}
			}
64
65
		} else {
			await goto(`/error`);
66
67
68
69
70
		}

		await tick();
		loaded = true;
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
71
72
73
74
75
</script>

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

77
{#if loaded}
78
79
	<slot />
{/if}
80
81

<Toaster />