+layout.svelte 1.57 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 () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
14
		const resBackend = await fetch(`${WEBUI_API_BASE_URL}/`, {
15
16
17
18
19
20
21
22
23
24
25
26
27
28
			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;
			});

Timothy J. Baek's avatar
Timothy J. Baek committed
29
30
		console.log(resBackend);
		await config.set(resBackend);
31

Timothy J. Baek's avatar
Timothy J. Baek committed
32
33
		if ($config) {
			if ($config.auth) {
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
				if (localStorage.token) {
					const res = await fetch(`${WEBUI_API_BASE_URL}/auths`, {
						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;
						});

Timothy J. Baek's avatar
Timothy J. Baek committed
52
53
54
55
56
57
					if (res) {
						await user.set(res);
					} else {
						localStorage.removeItem('token');
						await goto('/auth');
					}
58
				} else {
Timothy J. Baek's avatar
Timothy J. Baek committed
59
					await goto('/auth');
60
61
62
63
64
65
66
				}
			}
		}

		await tick();
		loaded = true;
	});
Timothy J. Baek's avatar
Timothy J. Baek committed
67
68
69
70
71
72
</script>

<svelte:head>
	<title>Ollama</title>
</svelte:head>
<Toaster />
73
74
75
76

{#if $config !== undefined && loaded}
	<slot />
{/if}