+layout.svelte 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <script>
  2. import { onMount, tick } from 'svelte';
  3. import { config, user } from '$lib/stores';
  4. import { goto } from '$app/navigation';
  5. import { WEBUI_API_BASE_URL } from '$lib/constants';
  6. import toast, { Toaster } from 'svelte-french-toast';
  7. import '../app.css';
  8. import '../tailwind.css';
  9. import 'tippy.js/dist/tippy.css';
  10. let loaded = false;
  11. onMount(async () => {
  12. const resBackend = await fetch(`${WEBUI_API_BASE_URL}/`, {
  13. method: 'GET',
  14. headers: {
  15. 'Content-Type': 'application/json'
  16. }
  17. })
  18. .then(async (res) => {
  19. if (!res.ok) throw await res.json();
  20. return res.json();
  21. })
  22. .catch((error) => {
  23. console.log(error);
  24. return null;
  25. });
  26. console.log(resBackend);
  27. await config.set(resBackend);
  28. if ($config) {
  29. if ($config.auth) {
  30. if (localStorage.token) {
  31. const res = await fetch(`${WEBUI_API_BASE_URL}/auths`, {
  32. method: 'GET',
  33. headers: {
  34. 'Content-Type': 'application/json',
  35. Authorization: `Bearer ${localStorage.token}`
  36. }
  37. })
  38. .then(async (res) => {
  39. if (!res.ok) throw await res.json();
  40. return res.json();
  41. })
  42. .catch((error) => {
  43. console.log(error);
  44. toast.error(error.detail);
  45. return null;
  46. });
  47. if (res) {
  48. await user.set(res);
  49. } else {
  50. localStorage.removeItem('token');
  51. await goto('/auth');
  52. }
  53. } else {
  54. await goto('/auth');
  55. }
  56. }
  57. }
  58. await tick();
  59. loaded = true;
  60. });
  61. </script>
  62. <svelte:head>
  63. <title>Ollama</title>
  64. </svelte:head>
  65. <Toaster />
  66. {#if $config !== undefined && loaded}
  67. <slot />
  68. {/if}