+page.svelte 5.02 KB
Newer Older
1
2
<script>
	import { goto } from '$app/navigation';
Timothy J. Baek's avatar
Timothy J. Baek committed
3
	import { userSignIn, userSignUp } from '$lib/apis/auths';
4
5
	import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
	import { WEBUI_NAME, config, user } from '$lib/stores';
6
	import { onMount, getContext } from 'svelte';
Jannik Streidl's avatar
Jannik Streidl committed
7
	import { toast } from 'svelte-sonner';
8

9
10
	const i18n = getContext('i18n');

Timothy J. Baek's avatar
Timothy J. Baek committed
11
	let loaded = false;
12
13
14
15
16
17
	let mode = 'signin';

	let name = '';
	let email = '';
	let password = '';

18
19
	let showPasswordField = !($config?.trusted_header_auth ?? false);

Timothy J. Baek's avatar
Timothy J. Baek committed
20
21
22
	const setSessionUser = async (sessionUser) => {
		if (sessionUser) {
			console.log(sessionUser);
23
			toast.success($i18n.t(`You're now logged in.`));
Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
			localStorage.token = sessionUser.token;
			await user.set(sessionUser);
26
27
28
29
			goto('/');
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
30
31
32
33
34
35
36
37
38
	const signInHandler = async () => {
		const sessionUser = await userSignIn(email, password).catch((error) => {
			toast.error(error);
			return null;
		});

		await setSessionUser(sessionUser);
	};

39
	const signUpHandler = async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
40
41
42
43
44
45
46
47
48
49
50
51
52
		const sessionUser = await userSignUp(name, email, password).catch((error) => {
			toast.error(error);
			return null;
		});

		await setSessionUser(sessionUser);
	};

	const submitHandler = async () => {
		if (mode === 'signin') {
			await signInHandler();
		} else {
			await signUpHandler();
53
54
55
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
56
	onMount(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
57
		if ($user !== undefined) {
Timothy J. Baek's avatar
Timothy J. Baek committed
58
59
60
61
			await goto('/');
		}
		loaded = true;
	});
62
63
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
64
65
66
67
68
69
<svelte:head>
	<title>
		{`${$WEBUI_NAME}`}
	</title>
</svelte:head>

Timothy J. Baek's avatar
Timothy J. Baek committed
70
{#if loaded}
71
72
73
	<div class="fixed m-10 z-50">
		<div class="flex space-x-2">
			<div class=" self-center">
74
				<img src="{WEBUI_BASE_URL}/static/favicon.png" class=" w-8 rounded-full" alt="logo" />
75
76
77
78
			</div>
		</div>
	</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
79
	<div class=" bg-white dark:bg-gray-900 min-h-screen w-full flex justify-center font-mona">
Timothy J. Baek's avatar
Timothy J. Baek committed
80
		<!-- <div class="hidden lg:flex lg:flex-1 px-10 md:px-16 w-full bg-yellow-50 justify-center">
81
82
83
84
85
86
87
88
89
90
91
			<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>
Timothy J. Baek's avatar
Timothy J. Baek committed
92
		</div> -->
93

Timothy J. Baek's avatar
Timothy J. Baek committed
94
		<div class="w-full sm:max-w-lg px-4 min-h-screen flex flex-col">
95
96
			<div class=" my-auto pb-10 w-full">
				<form
Timothy J. Baek's avatar
Timothy J. Baek committed
97
					class=" flex flex-col justify-center bg-white py-6 sm:py-16 px-6 sm:px-16 rounded-2xl"
98
					on:submit|preventDefault={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
99
						submitHandler();
100
101
					}}
				>
Timothy J. Baek's avatar
Timothy J. Baek committed
102
					<div class=" text-xl sm:text-2xl font-bold">
Jannik Streidl's avatar
Jannik Streidl committed
103
104
105
						{mode === 'signin' ? $i18n.t('Sign in') : $i18n.t('Sign up')}
						{$i18n.t('to')}
						{$WEBUI_NAME}
106
107
					</div>

108
109
					{#if mode === 'signup'}
						<div class=" mt-1 text-xs font-medium text-gray-500">
Jannik Streidl's avatar
Jannik Streidl committed
110
111
112
113
							ⓘ {$WEBUI_NAME}
							{$i18n.t(
								'does not make any external connections, and your data stays securely on your locally hosted server.'
							)}
114
115
116
						</div>
					{/if}

Timothy J. Baek's avatar
Timothy J. Baek committed
117
					<div class="flex flex-col mt-4">
118
119
						{#if mode === 'signup'}
							<div>
120
								<div class=" text-sm font-semibold text-left mb-1">{$i18n.t('Name')}</div>
121
122
123
								<input
									bind:value={name}
									type="text"
Timothy J. Baek's avatar
Timothy J. Baek committed
124
									class=" border px-4 py-2.5 rounded-2xl w-full text-sm"
125
									autocomplete="name"
Jannik Streidl's avatar
Jannik Streidl committed
126
									placeholder={$i18n.t('Enter Your Full Name')}
127
128
129
									required
								/>
							</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
130
131

							<hr class=" my-3" />
132
133
						{/if}

Timothy J. Baek's avatar
Timothy J. Baek committed
134
						<div class="mb-2">
135
							<div class=" text-sm font-semibold text-left mb-1">{$i18n.t('Email')}</div>
136
137
138
							<input
								bind:value={email}
								type="email"
Timothy J. Baek's avatar
Timothy J. Baek committed
139
								class=" border px-4 py-2.5 rounded-2xl w-full text-sm"
140
								autocomplete="email"
Jannik Streidl's avatar
Jannik Streidl committed
141
								placeholder={$i18n.t('Enter Your Email')}
142
143
144
145
								required
							/>
						</div>

146
147
148
149
150
151
152
153
154
155
156
157
158
						{#if showPasswordField}
							<div>
								<div class=" text-sm font-semibold text-left mb-1">{$i18n.t('Password')}</div>
								<input
									bind:value={password}
									type="password"
									class=" border px-4 py-2.5 rounded-2xl w-full text-sm"
									placeholder={$i18n.t('Enter Your Password')}
									autocomplete="current-password"
									required
								/>
							</div>
						{/if}
159
160
					</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
161
					<div class="mt-5">
162
						<button
Timothy J. Baek's avatar
Timothy J. Baek committed
163
							class=" bg-gray-900 hover:bg-gray-800 w-full rounded-full text-white font-semibold text-sm py-3 transition"
164
165
							type="submit"
						>
Jannik Streidl's avatar
Jannik Streidl committed
166
							{mode === 'signin' ? $i18n.t('Sign in') : $i18n.t('Create Account')}
167
168
169
						</button>

						<div class=" mt-4 text-sm text-center">
Jannik Streidl's avatar
Jannik Streidl committed
170
171
172
							{mode === 'signin'
								? $i18n.t("Don't have an account?")
								: $i18n.t('Already have an account?')}
173
174
175
176
177
178
179
180
181
182
183
184

							<button
								class=" font-medium underline"
								type="button"
								on:click={() => {
									if (mode === 'signin') {
										mode = 'signup';
									} else {
										mode = 'signin';
									}
								}}
							>
Jannik Streidl's avatar
Jannik Streidl committed
185
								{mode === 'signin' ? $i18n.t('Sign up') : $i18n.t('Sign in')}
186
187
188
189
190
191
192
193
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
194
195
196
197
198
199

<style>
	.font-mona {
		font-family: 'Mona Sans';
	}
</style>