open-webui/src/routes/auth/+page.svelte

226 lines
6.0 KiB
Svelte
Raw Normal View History

2023-11-19 00:47:12 +00:00
<script>
import { goto } from '$app/navigation';
2023-12-26 19:22:09 +00:00
import { userSignIn, userSignUp } from '$lib/apis/auths';
2024-03-29 20:02:38 +00:00
import Spinner from '$lib/components/common/Spinner.svelte';
2024-02-24 01:12:19 +00:00
import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
import { WEBUI_NAME, config, user } from '$lib/stores';
import { onMount, getContext } from 'svelte';
2024-03-01 09:18:07 +00:00
import { toast } from 'svelte-sonner';
import { generateInitialsImage, canvasPixelTest } from '$lib/utils';
2023-11-19 00:47:12 +00:00
const i18n = getContext('i18n');
2023-11-19 05:41:43 +00:00
let loaded = false;
2023-11-19 00:47:12 +00:00
let mode = 'signin';
let name = '';
let email = '';
let password = '';
2023-12-26 19:22:09 +00:00
const setSessionUser = async (sessionUser) => {
if (sessionUser) {
console.log(sessionUser);
toast.success($i18n.t(`You're now logged in.`));
2023-12-26 19:22:09 +00:00
localStorage.token = sessionUser.token;
await user.set(sessionUser);
2023-11-19 00:47:12 +00:00
goto('/');
}
};
2023-12-26 19:22:09 +00:00
const signInHandler = async () => {
const sessionUser = await userSignIn(email, password).catch((error) => {
toast.error(error);
return null;
});
await setSessionUser(sessionUser);
};
2023-11-19 00:47:12 +00:00
const signUpHandler = async () => {
2024-04-04 19:10:45 +00:00
const sessionUser = await userSignUp(name, email, password, generateInitialsImage(name)).catch(
(error) => {
toast.error(error);
return null;
}
);
2023-12-26 19:22:09 +00:00
await setSessionUser(sessionUser);
};
const submitHandler = async () => {
if (mode === 'signin') {
await signInHandler();
} else {
await signUpHandler();
2023-11-19 00:47:12 +00:00
}
};
2023-11-19 05:41:43 +00:00
onMount(async () => {
2023-12-26 19:22:09 +00:00
if ($user !== undefined) {
2023-11-19 05:41:43 +00:00
await goto('/');
}
loaded = true;
if ($config?.trusted_header_auth ?? false) {
await signInHandler();
}
2023-11-19 05:41:43 +00:00
});
2023-11-19 00:47:12 +00:00
</script>
2024-03-02 06:38:04 +00:00
<svelte:head>
<title>
{`${$WEBUI_NAME}`}
</title>
</svelte:head>
2023-12-26 19:22:09 +00:00
{#if loaded}
2023-11-19 00:47:12 +00:00
<div class="fixed m-10 z-50">
<div class="flex space-x-2">
<div class=" self-center">
2024-02-24 01:12:19 +00:00
<img src="{WEBUI_BASE_URL}/static/favicon.png" class=" w-8 rounded-full" alt="logo" />
2023-11-19 00:47:12 +00:00
</div>
</div>
</div>
2024-05-04 09:05:09 +00:00
<div class=" bg-white dark:bg-gray-950 min-h-screen w-full flex justify-center font-mona">
2023-12-26 19:22:09 +00:00
<!-- <div class="hidden lg:flex lg:flex-1 px-10 md:px-16 w-full bg-yellow-50 justify-center">
2023-11-19 00:47:12 +00:00
<div class=" my-auto pb-16 text-left">
<div>
<div class=" font-bold text-yellow-600 text-4xl">
Get up and running with <br />large language models, locally.
</div>
<div class="mt-2 text-yellow-600 text-xl">
Run Llama 2, Code Llama, and other models. Customize and create your own.
</div>
</div>
</div>
2023-12-26 19:22:09 +00:00
</div> -->
2023-11-19 00:47:12 +00:00
2024-05-04 09:05:09 +00:00
<div class="w-full sm:max-w-md px-10 min-h-screen flex flex-col text-center">
2024-03-29 20:02:38 +00:00
{#if $config?.trusted_header_auth ?? false}
<div class=" my-auto pb-10 w-full">
2024-03-29 20:02:38 +00:00
<div
class="flex items-center justify-center gap-3 text-xl sm:text-2xl text-center font-bold dark:text-gray-200"
>
<div>
{$i18n.t('Signing in')}
{$i18n.t('to')}
{$WEBUI_NAME}
</div>
<div>
<Spinner />
</div>
</div>
</div>
{:else}
2024-05-04 09:05:09 +00:00
<div class=" my-auto pb-10 w-full dark:text-gray-100">
2024-03-29 20:02:38 +00:00
<form
2024-05-04 09:05:09 +00:00
class=" flex flex-col justify-center"
2024-03-29 20:02:38 +00:00
on:submit|preventDefault={() => {
submitHandler();
}}
>
2024-05-04 09:05:09 +00:00
<div class="mb-1">
<div class=" text-2xl font-bold">
{mode === 'signin' ? $i18n.t('Sign in') : $i18n.t('Sign up')}
{$i18n.t('to')}
{$WEBUI_NAME}
2024-03-29 20:02:38 +00:00
</div>
2024-05-04 09:05:09 +00:00
{#if mode === 'signup'}
<div class=" mt-1 text-xs font-medium text-gray-500">
{$WEBUI_NAME}
{$i18n.t(
'does not make any external connections, and your data stays securely on your locally hosted server.'
)}
</div>
{/if}
</div>
2024-03-29 20:02:38 +00:00
<div class="flex flex-col mt-4">
{#if mode === 'signup'}
<div>
<div class=" text-sm font-semibold text-left mb-1">{$i18n.t('Name')}</div>
<input
bind:value={name}
type="text"
2024-05-04 09:05:09 +00:00
class=" px-5 py-3 rounded-2xl w-full text-sm border dark:border-none dark:bg-gray-900"
2024-03-29 20:02:38 +00:00
autocomplete="name"
placeholder={$i18n.t('Enter Your Full Name')}
required
/>
</div>
2024-05-04 09:05:09 +00:00
<hr class=" my-3 dark:border-gray-900" />
2024-03-29 20:02:38 +00:00
{/if}
<div class="mb-2">
<div class=" text-sm font-semibold text-left mb-1">{$i18n.t('Email')}</div>
2023-11-19 00:47:12 +00:00
<input
2024-03-29 20:02:38 +00:00
bind:value={email}
type="email"
2024-05-04 09:05:09 +00:00
class=" px-5 py-3 rounded-2xl w-full text-sm border dark:border-none dark:bg-gray-900"
2024-03-29 20:02:38 +00:00
autocomplete="email"
placeholder={$i18n.t('Enter Your Email')}
2023-11-19 00:47:12 +00:00
required
/>
</div>
2023-12-26 19:22:09 +00:00
2024-03-29 20:02:38 +00:00
<div>
<div class=" text-sm font-semibold text-left mb-1">{$i18n.t('Password')}</div>
2024-05-04 09:05:09 +00:00
2024-03-29 20:02:38 +00:00
<input
bind:value={password}
type="password"
2024-05-04 09:05:09 +00:00
class=" px-5 py-3 rounded-2xl w-full text-sm border dark:border-none dark:bg-gray-900"
2024-03-29 20:02:38 +00:00
placeholder={$i18n.t('Enter Your Password')}
autocomplete="current-password"
required
/>
</div>
</div>
2023-11-19 00:47:12 +00:00
2024-03-29 20:02:38 +00:00
<div class="mt-5">
2023-11-19 00:47:12 +00:00
<button
2024-05-04 09:05:09 +00:00
class=" bg-gray-900 hover:bg-gray-800 w-full rounded-2xl text-white font-semibold text-sm py-3 transition"
2024-03-29 20:02:38 +00:00
type="submit"
2023-11-19 00:47:12 +00:00
>
2024-03-29 20:02:38 +00:00
{mode === 'signin' ? $i18n.t('Sign in') : $i18n.t('Create Account')}
2023-11-19 00:47:12 +00:00
</button>
2024-03-29 20:02:38 +00:00
<div class=" mt-4 text-sm text-center">
{mode === 'signin'
? $i18n.t("Don't have an account?")
: $i18n.t('Already have an account?')}
<button
class=" font-medium underline"
type="button"
on:click={() => {
if (mode === 'signin') {
mode = 'signup';
} else {
mode = 'signin';
}
}}
>
{mode === 'signin' ? $i18n.t('Sign up') : $i18n.t('Sign in')}
</button>
</div>
2023-11-19 00:47:12 +00:00
</div>
2024-03-29 20:02:38 +00:00
</form>
</div>
{/if}
2023-11-19 00:47:12 +00:00
</div>
</div>
{/if}
2023-11-19 08:13:59 +00:00
<style>
.font-mona {
2024-04-27 23:42:55 +00:00
font-family: 'Mona Sans', -apple-system, 'Arimo', ui-sans-serif, system-ui, 'Segoe UI', Roboto,
Ubuntu, Cantarell, 'Noto Sans', sans-serif, 'Helvetica Neue', Arial, 'Apple Color Emoji',
'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
2023-11-19 08:13:59 +00:00
}
</style>