+page.svelte 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <script>
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, getContext } from 'svelte';
  4. import { goto } from '$app/navigation';
  5. import { page } from '$app/stores';
  6. import { getBackendConfig } from '$lib/apis';
  7. import { ldapUserSignIn, getSessionUser, userSignIn, userSignUp } from '$lib/apis/auths';
  8. import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
  9. import { WEBUI_NAME, config, user, socket } from '$lib/stores';
  10. import { generateInitialsImage, canvasPixelTest } from '$lib/utils';
  11. import Spinner from '$lib/components/common/Spinner.svelte';
  12. import OnBoarding from '$lib/components/OnBoarding.svelte';
  13. const i18n = getContext('i18n');
  14. let loaded = false;
  15. let mode = $config?.features.enable_ldap ? 'ldap' : 'signin';
  16. let name = '';
  17. let email = '';
  18. let password = '';
  19. let ldapUsername = '';
  20. const querystringValue = (key) => {
  21. const querystring = window.location.search;
  22. const urlParams = new URLSearchParams(querystring);
  23. return urlParams.get(key);
  24. };
  25. const setSessionUser = async (sessionUser) => {
  26. if (sessionUser) {
  27. console.log(sessionUser);
  28. toast.success($i18n.t(`You're now logged in.`));
  29. if (sessionUser.token) {
  30. localStorage.token = sessionUser.token;
  31. }
  32. $socket.emit('user-join', { auth: { token: sessionUser.token } });
  33. await user.set(sessionUser);
  34. await config.set(await getBackendConfig());
  35. const redirectPath = querystringValue('redirect') || '/';
  36. goto(redirectPath);
  37. }
  38. };
  39. const signInHandler = async () => {
  40. const sessionUser = await userSignIn(email, password).catch((error) => {
  41. toast.error(`${error}`);
  42. return null;
  43. });
  44. await setSessionUser(sessionUser);
  45. };
  46. const signUpHandler = async () => {
  47. const sessionUser = await userSignUp(name, email, password, generateInitialsImage(name)).catch(
  48. (error) => {
  49. toast.error(`${error}`);
  50. return null;
  51. }
  52. );
  53. await setSessionUser(sessionUser);
  54. };
  55. const ldapSignInHandler = async () => {
  56. const sessionUser = await ldapUserSignIn(ldapUsername, password).catch((error) => {
  57. toast.error(`${error}`);
  58. return null;
  59. });
  60. await setSessionUser(sessionUser);
  61. };
  62. const submitHandler = async () => {
  63. if (mode === 'ldap') {
  64. await ldapSignInHandler();
  65. } else if (mode === 'signin') {
  66. await signInHandler();
  67. } else {
  68. await signUpHandler();
  69. }
  70. };
  71. const checkOauthCallback = async () => {
  72. if (!$page.url.hash) {
  73. return;
  74. }
  75. const hash = $page.url.hash.substring(1);
  76. if (!hash) {
  77. return;
  78. }
  79. const params = new URLSearchParams(hash);
  80. const token = params.get('token');
  81. if (!token) {
  82. return;
  83. }
  84. const sessionUser = await getSessionUser(token).catch((error) => {
  85. toast.error(`${error}`);
  86. return null;
  87. });
  88. if (!sessionUser) {
  89. return;
  90. }
  91. localStorage.token = token;
  92. await setSessionUser(sessionUser);
  93. };
  94. let onboarding = false;
  95. onMount(async () => {
  96. if ($user !== undefined) {
  97. await goto('/');
  98. }
  99. await checkOauthCallback();
  100. loaded = true;
  101. if (($config?.features.auth_trusted_header ?? false) || $config?.features.auth === false) {
  102. await signInHandler();
  103. } else {
  104. onboarding = $config?.onboarding ?? false;
  105. }
  106. });
  107. </script>
  108. <svelte:head>
  109. <title>
  110. {`${$WEBUI_NAME}`}
  111. </title>
  112. </svelte:head>
  113. <OnBoarding
  114. bind:show={onboarding}
  115. getStartedHandler={() => {
  116. onboarding = false;
  117. mode = $config?.features.enable_ldap ? 'ldap' : 'signup';
  118. }}
  119. />
  120. <div class="w-full h-screen max-h-[100dvh] text-white relative">
  121. <div class="w-full h-full absolute top-0 left-0 bg-white dark:bg-black"></div>
  122. <div class="w-full absolute top-0 left-0 right-0 h-8 drag-region" />
  123. {#if loaded}
  124. <div class="fixed m-10 z-50">
  125. <div class="flex space-x-2">
  126. <div class=" self-center">
  127. <img
  128. crossorigin="anonymous"
  129. src="{WEBUI_BASE_URL}/static/splash.png"
  130. class=" w-6 rounded-full dark:invert"
  131. alt="logo"
  132. />
  133. </div>
  134. </div>
  135. </div>
  136. <div
  137. class="fixed bg-transparent min-h-screen w-full flex justify-center font-primary z-50 text-black dark:text-white"
  138. >
  139. <div class="w-full sm:max-w-md px-10 min-h-screen flex flex-col text-center">
  140. {#if ($config?.features.auth_trusted_header ?? false) || $config?.features.auth === false}
  141. <div class=" my-auto pb-10 w-full">
  142. <div
  143. class="flex items-center justify-center gap-3 text-xl sm:text-2xl text-center font-semibold dark:text-gray-200"
  144. >
  145. <div>
  146. {$i18n.t('Signing in to {{WEBUI_NAME}}', { WEBUI_NAME: $WEBUI_NAME })}
  147. </div>
  148. <div>
  149. <Spinner />
  150. </div>
  151. </div>
  152. </div>
  153. {:else}
  154. <div class=" my-auto pb-10 w-full dark:text-gray-100">
  155. <form
  156. class=" flex flex-col justify-center"
  157. on:submit={(e) => {
  158. e.preventDefault();
  159. submitHandler();
  160. }}
  161. >
  162. <div class="mb-1">
  163. <div class=" text-2xl font-medium">
  164. {#if $config?.onboarding ?? false}
  165. {$i18n.t(`Get started with {{WEBUI_NAME}}`, { WEBUI_NAME: $WEBUI_NAME })}
  166. {:else if mode === 'ldap'}
  167. {$i18n.t(`Sign in to {{WEBUI_NAME}} with LDAP`, { WEBUI_NAME: $WEBUI_NAME })}
  168. {:else if mode === 'signin'}
  169. {$i18n.t(`Sign in to {{WEBUI_NAME}}`, { WEBUI_NAME: $WEBUI_NAME })}
  170. {:else}
  171. {$i18n.t(`Sign up to {{WEBUI_NAME}}`, { WEBUI_NAME: $WEBUI_NAME })}
  172. {/if}
  173. </div>
  174. {#if $config?.onboarding ?? false}
  175. <div class=" mt-1 text-xs font-medium text-gray-500">
  176. ⓘ {$WEBUI_NAME}
  177. {$i18n.t(
  178. 'does not make any external connections, and your data stays securely on your locally hosted server.'
  179. )}
  180. </div>
  181. {/if}
  182. </div>
  183. {#if $config?.features.enable_login_form || $config?.features.enable_ldap}
  184. <div class="flex flex-col mt-4">
  185. {#if mode === 'signup'}
  186. <div class="mb-2">
  187. <div class=" text-sm font-medium text-left mb-1">{$i18n.t('Name')}</div>
  188. <input
  189. bind:value={name}
  190. type="text"
  191. class="my-0.5 w-full text-sm outline-hidden bg-transparent"
  192. autocomplete="name"
  193. placeholder={$i18n.t('Enter Your Full Name')}
  194. required
  195. />
  196. </div>
  197. {/if}
  198. {#if mode === 'ldap'}
  199. <div class="mb-2">
  200. <div class=" text-sm font-medium text-left mb-1">{$i18n.t('Username')}</div>
  201. <input
  202. bind:value={ldapUsername}
  203. type="text"
  204. class="my-0.5 w-full text-sm outline-hidden bg-transparent"
  205. autocomplete="username"
  206. name="username"
  207. placeholder={$i18n.t('Enter Your Username')}
  208. required
  209. />
  210. </div>
  211. {:else}
  212. <div class="mb-2">
  213. <div class=" text-sm font-medium text-left mb-1">{$i18n.t('Email')}</div>
  214. <input
  215. bind:value={email}
  216. type="email"
  217. class="my-0.5 w-full text-sm outline-hidden bg-transparent"
  218. autocomplete="email"
  219. name="email"
  220. placeholder={$i18n.t('Enter Your Email')}
  221. required
  222. />
  223. </div>
  224. {/if}
  225. <div>
  226. <div class=" text-sm font-medium text-left mb-1">{$i18n.t('Password')}</div>
  227. <input
  228. bind:value={password}
  229. type="password"
  230. class="my-0.5 w-full text-sm outline-hidden bg-transparent"
  231. placeholder={$i18n.t('Enter Your Password')}
  232. autocomplete="current-password"
  233. name="current-password"
  234. required
  235. />
  236. </div>
  237. </div>
  238. {/if}
  239. <div class="mt-5">
  240. {#if $config?.features.enable_login_form || $config?.features.enable_ldap}
  241. {#if mode === 'ldap'}
  242. <button
  243. class="bg-gray-700/5 hover:bg-gray-700/10 dark:bg-gray-100/5 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition w-full rounded-full font-medium text-sm py-2.5"
  244. type="submit"
  245. >
  246. {$i18n.t('Authenticate')}
  247. </button>
  248. {:else}
  249. <button
  250. class="bg-gray-700/5 hover:bg-gray-700/10 dark:bg-gray-100/5 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition w-full rounded-full font-medium text-sm py-2.5"
  251. type="submit"
  252. >
  253. {mode === 'signin'
  254. ? $i18n.t('Sign in')
  255. : ($config?.onboarding ?? false)
  256. ? $i18n.t('Create Admin Account')
  257. : $i18n.t('Create Account')}
  258. </button>
  259. {#if $config?.features.enable_signup && !($config?.onboarding ?? false)}
  260. <div class=" mt-4 text-sm text-center">
  261. {mode === 'signin'
  262. ? $i18n.t("Don't have an account?")
  263. : $i18n.t('Already have an account?')}
  264. <button
  265. class=" font-medium underline"
  266. type="button"
  267. on:click={() => {
  268. if (mode === 'signin') {
  269. mode = 'signup';
  270. } else {
  271. mode = 'signin';
  272. }
  273. }}
  274. >
  275. {mode === 'signin' ? $i18n.t('Sign up') : $i18n.t('Sign in')}
  276. </button>
  277. </div>
  278. {/if}
  279. {/if}
  280. {/if}
  281. </div>
  282. </form>
  283. {#if Object.keys($config?.oauth?.providers ?? {}).length > 0}
  284. <div class="inline-flex items-center justify-center w-full">
  285. <hr class="w-32 h-px my-4 border-0 dark:bg-gray-100/10 bg-gray-700/10" />
  286. {#if $config?.features.enable_login_form || $config?.features.enable_ldap}
  287. <span
  288. class="px-3 text-sm font-medium text-gray-900 dark:text-white bg-transparent"
  289. >{$i18n.t('or')}</span
  290. >
  291. {/if}
  292. <hr class="w-32 h-px my-4 border-0 dark:bg-gray-100/10 bg-gray-700/10" />
  293. </div>
  294. <div class="flex flex-col space-y-2">
  295. {#if $config?.oauth?.providers?.google}
  296. <button
  297. class="flex justify-center items-center bg-gray-700/5 hover:bg-gray-700/10 dark:bg-gray-100/5 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition w-full rounded-full font-medium text-sm py-2.5"
  298. on:click={() => {
  299. window.location.href = `${WEBUI_BASE_URL}/oauth/google/login`;
  300. }}
  301. >
  302. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" class="size-6 mr-3">
  303. <path
  304. fill="#EA4335"
  305. d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"
  306. /><path
  307. fill="#4285F4"
  308. d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"
  309. /><path
  310. fill="#FBBC05"
  311. d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"
  312. /><path
  313. fill="#34A853"
  314. d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"
  315. /><path fill="none" d="M0 0h48v48H0z" />
  316. </svg>
  317. <span>{$i18n.t('Continue with {{provider}}', { provider: 'Google' })}</span>
  318. </button>
  319. {/if}
  320. {#if $config?.oauth?.providers?.microsoft}
  321. <button
  322. class="flex justify-center items-center bg-gray-700/5 hover:bg-gray-700/10 dark:bg-gray-100/5 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition w-full rounded-full font-medium text-sm py-2.5"
  323. on:click={() => {
  324. window.location.href = `${WEBUI_BASE_URL}/oauth/microsoft/login`;
  325. }}
  326. >
  327. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 21 21" class="size-6 mr-3">
  328. <rect x="1" y="1" width="9" height="9" fill="#f25022" /><rect
  329. x="1"
  330. y="11"
  331. width="9"
  332. height="9"
  333. fill="#00a4ef"
  334. /><rect x="11" y="1" width="9" height="9" fill="#7fba00" /><rect
  335. x="11"
  336. y="11"
  337. width="9"
  338. height="9"
  339. fill="#ffb900"
  340. />
  341. </svg>
  342. <span>{$i18n.t('Continue with {{provider}}', { provider: 'Microsoft' })}</span>
  343. </button>
  344. {/if}
  345. {#if $config?.oauth?.providers?.github}
  346. <button
  347. class="flex justify-center items-center bg-gray-700/5 hover:bg-gray-700/10 dark:bg-gray-100/5 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition w-full rounded-full font-medium text-sm py-2.5"
  348. on:click={() => {
  349. window.location.href = `${WEBUI_BASE_URL}/oauth/github/login`;
  350. }}
  351. >
  352. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="size-6 mr-3">
  353. <path
  354. fill="currentColor"
  355. d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.92 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57C20.565 21.795 24 17.31 24 12c0-6.63-5.37-12-12-12z"
  356. />
  357. </svg>
  358. <span>{$i18n.t('Continue with {{provider}}', { provider: 'GitHub' })}</span>
  359. </button>
  360. {/if}
  361. {#if $config?.oauth?.providers?.oidc}
  362. <button
  363. class="flex justify-center items-center bg-gray-700/5 hover:bg-gray-700/10 dark:bg-gray-100/5 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition w-full rounded-full font-medium text-sm py-2.5"
  364. on:click={() => {
  365. window.location.href = `${WEBUI_BASE_URL}/oauth/oidc/login`;
  366. }}
  367. >
  368. <svg
  369. xmlns="http://www.w3.org/2000/svg"
  370. fill="none"
  371. viewBox="0 0 24 24"
  372. stroke-width="1.5"
  373. stroke="currentColor"
  374. class="size-6 mr-3"
  375. >
  376. <path
  377. stroke-linecap="round"
  378. stroke-linejoin="round"
  379. d="M15.75 5.25a3 3 0 0 1 3 3m3 0a6 6 0 0 1-7.029 5.912c-.563-.097-1.159.026-1.563.43L10.5 17.25H8.25v2.25H6v2.25H2.25v-2.818c0-.597.237-1.17.659-1.591l6.499-6.499c.404-.404.527-1 .43-1.563A6 6 0 1 1 21.75 8.25Z"
  380. />
  381. </svg>
  382. <span
  383. >{$i18n.t('Continue with {{provider}}', {
  384. provider: $config?.oauth?.providers?.oidc ?? 'SSO'
  385. })}</span
  386. >
  387. </button>
  388. {/if}
  389. </div>
  390. {/if}
  391. {#if $config?.features.enable_ldap && $config?.features.enable_login_form}
  392. <div class="mt-2">
  393. <button
  394. class="flex justify-center items-center text-xs w-full text-center underline"
  395. type="button"
  396. on:click={() => {
  397. if (mode === 'ldap')
  398. mode = ($config?.onboarding ?? false) ? 'signup' : 'signin';
  399. else mode = 'ldap';
  400. }}
  401. >
  402. <span
  403. >{mode === 'ldap'
  404. ? $i18n.t('Continue with Email')
  405. : $i18n.t('Continue with LDAP')}</span
  406. >
  407. </button>
  408. </div>
  409. {/if}
  410. </div>
  411. {/if}
  412. </div>
  413. </div>
  414. {/if}
  415. </div>