File size: 13,386 Bytes
b4668c2
7c43dbf
d433b55
48059af
566c2fc
b4668c2
e99e7c2
e273a27
 
 
 
 
 
e99e7c2
e273a27
7bf1507
e273a27
 
 
245b57f
e273a27
245b57f
e273a27
 
 
 
 
 
 
 
 
 
 
245b57f
e273a27
245b57f
e273a27
 
 
7bf1507
e273a27
 
 
 
245b57f
e273a27
 
 
245b57f
e273a27
 
245b57f
e273a27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245b57f
e273a27
245b57f
 
b4668c2
 
7bf1507
911412b
a8a9533
911412b
 
67577d7
48059af
911412b
245b57f
e273a27
 
 
 
 
 
 
245b57f
e273a27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245b57f
e273a27
 
 
245b57f
e273a27
 
 
 
 
245b57f
e273a27
 
566c2fc
e273a27
 
 
566c2fc
e273a27
566c2fc
e273a27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
911412b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<script lang="ts">
	import { base } from "$app/paths";
	import { page } from "$app/state";

	import LogoHuggingFaceBorderless from "$lib/components/icons/LogoHuggingFaceBorderless.svelte";
	import Modal from "$lib/components/Modal.svelte";
	import { cookiesAreEnabled } from "$lib/utils/cookiesAreEnabled";
	import Logo from "./icons/Logo.svelte";
	import CarbonCopy from "~icons/carbon/copy";
	import CarbonCheckmark from "~icons/carbon/checkmark";
	import CarbonView from "~icons/carbon/view";
	import CarbonViewOff from "~icons/carbon/view-off";
	import { usePublicConfig } from "$lib/utils/PublicConfig.svelte";

	const publicConfig = usePublicConfig();

	interface Props {
		onclose?: () => void;
	}

	let { onclose }: Props = $props();

	let showPasswordForm = $state(false);
	let isRegistering = $state(false);
	let isRecovering = $state(false);
	let passwordErrorMessage = $state("");
	let passwordSuccessMessage = $state("");
	let isSubmitting = $state(false);
	
	let recoveryKey = $state("");
	let recoveryKeyCopied = $state(false);
	let passwordVisible = $state(false);
	let passwordInput = $state("");

	let usernameInput = $state("");

	async function handlePasswordSubmit(event: SubmitEvent) {
		event.preventDefault();
		if (isSubmitting) return;

		if (!cookiesAreEnabled()) {
			window.open(window.location.href, "_blank");
			return;
		}

		isSubmitting = true;
		passwordErrorMessage = "";
		passwordSuccessMessage = "";

		const formData = new FormData(event.target as HTMLFormElement);
		const body = Object.fromEntries(formData.entries());

		try {
			if (isRecovering) {
				const response = await fetch(`${base}/login/recover`, {
					method: "POST",
					headers: { "Content-Type": "application/json" },
					body: JSON.stringify(body),
				});

				if (response.ok) {
					const data = await response.json();
					if (data.newRecoveryKey) {
						isRecovering = false;
						recoveryKey = data.newRecoveryKey;
						passwordSuccessMessage = "Password reset successfully. Please save your NEW recovery key.";
					} else {
						isRecovering = false;
						isRegistering = false;
						passwordSuccessMessage = "Password reset successfully. Please sign in.";
					}
				} else {
					const result = await response.json();
					passwordErrorMessage = result.message || "Recovery failed";
				}
			} else if (isRegistering) {
				const response = await fetch(`${base}/login/register`, {
					method: "POST",
					headers: { "Content-Type": "application/json" },
					body: JSON.stringify(body),
				});

				if (response.ok) {
					const data = await response.json();
					recoveryKey = data.recoveryKey;
				} else {
					const result = await response.json();
					passwordErrorMessage = result.message || "Registration failed";
				}
			} else {
				const response = await fetch(`${base}/login/password`, {
					method: "POST",
					headers: { "Content-Type": "application/json" },
					body: JSON.stringify(body),
				});

				if (response.ok) {
					window.location.href = `${base}/`;
				} else {
					const result = await response.json();
					passwordErrorMessage = result.message || "Invalid username or password";
				}
			}
		} catch (e) {
			console.error(e);
			passwordErrorMessage = "An error occurred. Please try again.";
		} finally {
			isSubmitting = false;
		}
	}

	function handleCopyRecoveryKey() {
		navigator.clipboard.writeText(recoveryKey);
		recoveryKeyCopied = true;
	}

	function handleCloseRecoveryModal() {
		if (passwordSuccessMessage && passwordSuccessMessage.includes("NEW recovery key")) {
			recoveryKey = "";
			recoveryKeyCopied = false;
			isRecovering = false;
			isRegistering = false;
			passwordSuccessMessage = "Password updated successfully. Please sign in with your new password.";
		} else {
			window.location.href = `${base}/`;
		}
	}
</script>

<Modal onclose={() => onclose?.()} width="!max-w-[400px] !m-4">
	<div
		class="from-primary-500/40 via-primary-500/10 to-primary-500/0 flex w-full flex-col items-center gap-6 bg-gradient-to-b px-5 pb-8 pt-9 text-center"
	>
		<h2 class="flex items-center text-2xl font-semibold text-gray-800">
			<Logo classNames="mr-1" />
			{publicConfig.PUBLIC_APP_NAME}
		</h2>

		{#if recoveryKey}
			<div class="flex w-full flex-col items-center gap-4">
				<h3 class="text-xl font-semibold text-gray-800">
					{#if passwordSuccessMessage && passwordSuccessMessage.includes("NEW recovery key")}
						Password Updated!
					{:else}
						Account Created!
					{/if}
				</h3>
				<p class="text-sm text-gray-600 text-balance">
					{#if passwordSuccessMessage && passwordSuccessMessage.includes("NEW recovery key")}
						Your password has been updated.
						<br/><br/>
						Your old recovery key has been invalidated. <strong>Save this NEW key safely.</strong> It is now the ONLY way to recover your account.
					{:else}
						Save this Recovery Key safely. <strong>It is the ONLY way to recover your account</strong> if you forget your password.
					{/if}
				</p>

				<div class="flex w-full items-center gap-2 rounded-md border border-gray-200 bg-gray-50 p-2">
					<code class="flex-1 break-all font-mono text-sm text-gray-800">
						{recoveryKey}
					</code>
					<button
						onclick={handleCopyRecoveryKey}
						class="flex h-8 w-8 flex-none items-center justify-center rounded-md text-gray-500 hover:bg-gray-200 hover:text-black transition-colors"
						title="Copy Recovery Key"
					>
						{#if recoveryKeyCopied}
							<CarbonCheckmark class="text-green-600" />
						{:else}
							<CarbonCopy />
						{/if}
					</button>
				</div>

				<button
					onclick={handleCloseRecoveryModal}
					disabled={!recoveryKeyCopied}
					class="mt-2 w-full rounded-full bg-black px-5 py-2 text-lg font-semibold text-gray-100 transition-colors hover:bg-gray-900 disabled:cursor-not-allowed disabled:opacity-50"
				>
					I have saved my key
				</button>
			</div>
		{:else}
			<div class="flex w-full flex-col items-center gap-3">
				{#if page.data.loginRequired}
					<a
						href="{base}/login"
						class="flex w-full flex-wrap items-center justify-center whitespace-nowrap rounded-full bg-black px-5 py-2 text-center text-lg font-semibold text-gray-100 transition-colors hover:bg-gray-900"
					>
						Sign in with
						<span class="flex items-center">
							&nbsp;<LogoHuggingFaceBorderless classNames="text-xl mr-1" /> Hugging Face
						</span>
					</a>

					{#if !showPasswordForm}
						<button
							type="button"
							onclick={() => {
								showPasswordForm = true;
								isRegistering = false;
								isRecovering = false;
								passwordInput = "";
								passwordErrorMessage = "";
								passwordSuccessMessage = "";
							}}
							class="flex w-full items-center justify-center whitespace-nowrap rounded-full border-2 border-black bg-white px-5 py-2 text-lg font-semibold text-gray-800 transition-colors hover:bg-gray-100"
						>
							Sign in with username/password
						</button>
					{/if}

					{#if showPasswordForm}
						<form
							onsubmit={handlePasswordSubmit}
							class="flex w-full flex-col gap-2 rounded-xl border border-gray-200 bg-white/80 p-3 text-left shadow-sm"
						>
							<div class="mb-2 text-center">
								<h3 class="text-lg font-semibold">
									{#if isRecovering}
										Reset Password
									{:else if isRegistering}
										Create Account
									{:else}
										Sign In
									{/if}
								</h3>
							</div>

							<label class="text-sm font-medium text-gray-700">
								Username
								<input
									type="text"
									name="username"
									autocomplete="username"
									required
									bind:value={usernameInput}
									class="mt-1 w-full rounded-md border border-gray-300 px-3 py-2 text-base text-gray-900 focus:border-black focus:outline-none focus:ring-2 focus:ring-black/50"
								/>
							</label>

							{#if isRecovering}
								<label class="text-sm font-medium text-gray-700">
									Recovery Key
									<input
										type="text"
										name="recoveryKey"
										autocomplete="off"
										required
										class="mt-1 w-full rounded-md border border-gray-300 px-3 py-2 text-base text-gray-900 focus:border-black focus:outline-none focus:ring-2 focus:ring-black/50"
									/>
								</label>
								<label class="text-sm font-medium text-gray-700">
									New Password
									<div class="relative mt-1">
										<input
											type={passwordVisible ? "text" : "password"}
											name="newPassword"
											autocomplete="new-password"
											required
											minlength="8"
											bind:value={passwordInput}
											class="w-full rounded-md border border-gray-300 pl-3 pr-20 py-2 text-base text-gray-900 focus:border-black focus:outline-none focus:ring-2 focus:ring-black/50"
										/>
										<div class="absolute right-2 top-1/2 -translate-y-1/2 flex items-center gap-1">
											<button
												type="button"
												class="p-1 text-gray-500 hover:text-black"
												onclick={() => (passwordVisible = !passwordVisible)}
												title={passwordVisible ? "Hide password" : "Show password"}
											>
												{#if passwordVisible}
													<CarbonViewOff />
												{:else}
													<CarbonView />
												{/if}
											</button>
										</div>
									</div>
								</label>
							{:else}
								{#if isRegistering}
									<label class="text-sm font-medium text-gray-700">
										Email (Optional)
										<input
											type="email"
											name="email"
											autocomplete="email"
											class="mt-1 w-full rounded-md border border-gray-300 px-3 py-2 text-base text-gray-900 focus:border-black focus:outline-none focus:ring-2 focus:ring-black/50"
										/>
									</label>
								{/if}

								<label class="text-sm font-medium text-gray-700">
									Password
									<div class="relative mt-1">
										<input
											type={passwordVisible ? "text" : "password"}
											name="password"
											autocomplete={isRegistering ? "new-password" : "current-password"}
											required
											minlength="8"
											bind:value={passwordInput}
											class="w-full rounded-md border border-gray-300 pl-3 pr-20 py-2 text-base text-gray-900 focus:border-black focus:outline-none focus:ring-2 focus:ring-black/50"
										/>
										<div class="absolute right-2 top-1/2 -translate-y-1/2 flex items-center gap-1">
											<button
												type="button"
												class="p-1 text-gray-500 hover:text-black"
												onclick={() => (passwordVisible = !passwordVisible)}
												title={passwordVisible ? "Hide password" : "Show password"}
											>
												{#if passwordVisible}
													<CarbonViewOff />
												{:else}
													<CarbonView />
												{/if}
											</button>
										</div>
									</div>
								</label>
							{/if}

							{#if passwordErrorMessage}
								<p class="text-sm text-red-600">{passwordErrorMessage}</p>
							{/if}
							{#if passwordSuccessMessage}
								<p class="text-sm text-green-600">{passwordSuccessMessage}</p>
							{/if}

							<button
								type="submit"
								disabled={isSubmitting}
								class="mt-1 flex w-full items-center justify-center whitespace-nowrap rounded-full bg-black px-4 py-2 text-lg font-semibold text-gray-100 transition-colors hover:bg-gray-900 disabled:cursor-not-allowed disabled:bg-gray-500"
							>
								{#if isSubmitting}
									Submitting...
								{:else}
									{#if isRecovering}
										Reset Password
									{:else if isRegistering}
										Create Account
									{:else}
										Sign In
									{/if}
								{/if}
							</button>

							{#if !isRecovering && !isRegistering}
								<div class="mt-1 text-center">
									<button type="button" onclick={() => { isRecovering = true; isRegistering = false; passwordInput = ""; passwordErrorMessage = ""; passwordSuccessMessage = ""; }} class="text-sm text-gray-500 hover:underline">Forgot Password?</button>
								</div>
							{/if}

							<div class="mt-2 text-center text-sm text-gray-600">
								{#if isRecovering}
									Remember it? <button type="button" onclick={() => { isRecovering = false; passwordInput = ""; passwordErrorMessage = ""; passwordSuccessMessage = ""; }} class="font-semibold underline">Sign in</button>
								{:else if isRegistering}
									Already have an account? <button type="button" onclick={() => { isRegistering = false; passwordInput = ""; passwordErrorMessage = ""; passwordSuccessMessage = ""; }} class="font-semibold underline">Sign in</button>
								{:else}
									Don't have an account? <button type="button" onclick={() => { isRegistering = true; passwordInput = ""; passwordErrorMessage = ""; passwordSuccessMessage = ""; }} class="font-semibold underline">Create one</button>
								{/if}
							</div>
						</form>
					{/if}
				{:else}
					<button
						class="flex w-full items-center justify-center whitespace-nowrap rounded-full border-2 border-black bg-black px-5 py-2 text-lg font-semibold text-gray-100 transition-colors hover:bg-gray-900"
						onclick={(e) => {
							if (!cookiesAreEnabled()) {
								e.preventDefault();
								window.open(window.location.href, "_blank");
							}
							onclose?.();
						}}
					>
						Start chatting
					</button>
				{/if}
			</div>
		{/if}
	</div>
</Modal>