+page.svelte 4.9 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 = '';

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

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

		await setSessionUser(sessionUser);
	};

37
	const signUpHandler = async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
38
39
40
41
42
43
44
45
46
47
48
49
50
		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();
51
52
53
		}
	};

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

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

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

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

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

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

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

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

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

						<div>
145
							<div class=" text-sm font-semibold text-left mb-1">{$i18n.t('Password')}</div>
146
147
148
							<input
								bind:value={password}
								type="password"
Timothy J. Baek's avatar
Timothy J. Baek committed
149
								class=" border px-4 py-2.5 rounded-2xl w-full text-sm"
Jannik Streidl's avatar
Jannik Streidl committed
150
								placeholder={$i18n.t('Enter Your Password')}
151
152
153
154
155
156
								autocomplete="current-password"
								required
							/>
						</div>
					</div>

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

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

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

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