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

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

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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
	const setSessionUser = async (sessionUser) => {
		if (sessionUser) {
			console.log(sessionUser);
22
			toast.success($i18n.t(`You're now logged in.`));
Timothy J. Baek's avatar
Timothy J. Baek committed
23
			localStorage.token = sessionUser.token;
liu.vaayne's avatar
liu.vaayne committed
24
			await setApiKey(sessionUser.token);
Timothy J. Baek's avatar
Timothy J. Baek committed
25
			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
			await goto('/');
		}
		loaded = true;
61
62
63
		if ($config?.trusted_header_auth ?? false) {
			await signInHandler();
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
64
	});
65
66
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
67
68
69
70
71
72
<svelte:head>
	<title>
		{`${$WEBUI_NAME}`}
	</title>
</svelte:head>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
82
	<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
83
		<!-- <div class="hidden lg:flex lg:flex-1 px-10 md:px-16 w-full bg-yellow-50 justify-center">
84
85
86
87
88
89
90
91
92
93
94
			<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
95
		</div> -->
96

Timothy J. Baek's avatar
Timothy J. Baek committed
97
		<div class="w-full sm:max-w-lg px-4 min-h-screen flex flex-col">
Timothy J. Baek's avatar
Timothy J. Baek committed
98
			{#if $config?.trusted_header_auth ?? false}
99
				<div class=" my-auto pb-10 w-full">
Timothy J. Baek's avatar
Timothy J. Baek committed
100
101
102
103
104
105
106
107
					<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>
108

Timothy J. Baek's avatar
Timothy J. Baek committed
109
110
111
						<div>
							<Spinner />
						</div>
112
113
114
115
					</div>
				</div>
			{:else}
				<div class=" my-auto pb-10 w-full">
Timothy J. Baek's avatar
Timothy J. Baek committed
116
117
118
119
120
121
122
123
124
125
					<form
						class=" flex flex-col justify-center bg-white py-6 sm:py-16 px-6 sm:px-16 rounded-2xl"
						on:submit|preventDefault={() => {
							submitHandler();
						}}
					>
						<div class=" text-xl sm:text-2xl font-bold">
							{mode === 'signin' ? $i18n.t('Sign in') : $i18n.t('Sign up')}
							{$i18n.t('to')}
							{$WEBUI_NAME}
126
127
						</div>

128
						{#if mode === 'signup'}
Timothy J. Baek's avatar
Timothy J. Baek committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
							<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 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"
										class=" border px-4 py-2.5 rounded-2xl w-full text-sm"
										autocomplete="name"
										placeholder={$i18n.t('Enter Your Full Name')}
										required
									/>
								</div>

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

							<div class="mb-2">
								<div class=" text-sm font-semibold text-left mb-1">{$i18n.t('Email')}</div>
156
								<input
Timothy J. Baek's avatar
Timothy J. Baek committed
157
158
									bind:value={email}
									type="email"
Timothy J. Baek's avatar
Timothy J. Baek committed
159
									class=" border px-4 py-2.5 rounded-2xl w-full text-sm"
Timothy J. Baek's avatar
Timothy J. Baek committed
160
161
									autocomplete="email"
									placeholder={$i18n.t('Enter Your Email')}
162
163
164
									required
								/>
							</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
165

Timothy J. Baek's avatar
Timothy J. Baek committed
166
167
168
169
170
171
172
173
174
175
176
							<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>
177
178
						</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
179
						<div class="mt-5">
180
							<button
Timothy J. Baek's avatar
Timothy J. Baek committed
181
182
								class=" bg-gray-900 hover:bg-gray-800 w-full rounded-full text-white font-semibold text-sm py-3 transition"
								type="submit"
183
							>
Timothy J. Baek's avatar
Timothy J. Baek committed
184
								{mode === 'signin' ? $i18n.t('Sign in') : $i18n.t('Create Account')}
185
							</button>
Timothy J. Baek's avatar
Timothy J. Baek committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

							<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>
206
						</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
207
208
					</form>
				</div>
209
			{/if}
210
211
212
		</div>
	</div>
{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
213
214
215
216
217
218

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