Files
Sharpbin/SharpbinV3.Client/src/routes/register/+page.svelte
T
2026-02-05 16:34:03 -05:00

326 lines
9.3 KiB
Svelte

<script lang="ts">
import { goto } from '$app/navigation';
import { setTokens, startTokenRefreshInterval } from '$lib/utils/auth';
import { extractError } from '$lib/utils/misc';
import { Check, X, Eye, EyeClosed } from '@lucide/svelte';
import { slide, fade } from 'svelte/transition';
import { resolve } from '$app/paths';
import { onMount } from 'svelte';
import type { PageData } from './$types';
let username = '';
let password = '';
let confirmPassword = '';
let email = '';
let displayName = '';
let error = '';
let passwordFocused = false;
let showPassword = false;
let passwordChecks = {
minLength: false,
maxLength: false,
upper: false,
lower: false,
number: false
};
let passwordValid = false;
let loading = false;
export let data: PageData;
onMount(() => {
const render = () => {
if (
window.turnstile &&
data.options?.cf_turnstile_site_key &&
data.options?.registration_enabled
) {
window.turnstile.render('.cf-turnstile', {
sitekey: data.options.cf_turnstile_site_key,
theme: 'dark',
size: 'flexible',
appearence: 'interaction-only'
});
}
};
if (window.turnstile) {
render();
} else {
window.addEventListener('turnstile:loaded', render, { once: true });
}
return () => {
window.removeEventListener('turnstile:loaded', render);
};
});
function validatePassword(pw: string) {
passwordChecks.minLength = pw.length >= 8;
passwordChecks.maxLength = pw.length <= 128;
passwordChecks.upper = /[A-Z]/.test(pw);
passwordChecks.lower = /[a-z]/.test(pw);
passwordChecks.number = /[0-9]/.test(pw);
passwordValid =
passwordChecks.minLength &&
passwordChecks.maxLength &&
passwordChecks.upper &&
passwordChecks.lower &&
passwordChecks.number;
}
$: validatePassword(password);
async function register() {
if (!passwordValid || password !== confirmPassword) return;
loading = true;
error = '';
const body: Record<string, unknown> = { username, password, email, displayName };
if (data.options?.cf_turnstile_site_key && window.turnstile) {
const token = window.turnstile.getResponse();
if (token) body.token = token;
}
const res = await fetch('/api/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
if (res.ok) {
const data = await res.json();
if (data.token && data.refreshToken) {
setTokens(data.token, data.refreshToken);
startTokenRefreshInterval();
goto(resolve('/'));
} else {
const urlParams = new URLSearchParams(window.location.search);
const returnUrl = urlParams.get('return');
if (returnUrl) {
goto(resolve(`/login?return=${encodeURIComponent(returnUrl)}`));
} else {
goto(resolve('/login'));
}
}
} else {
const resData = await res.json();
if (window.turnstile) {
window.turnstile.reset();
}
if (resData.errors) {
const messages = Object.values(resData.errors).flat();
error = messages.join('\n');
} else {
error = extractError(resData) || 'Registration failed. Please try again.';
if (error === 'Verification failed.') {
if (window.turnstile) {
window.turnstile.reset();
}
}
}
}
loading = false;
}
</script>
<svelte:head>
<title>Register - Sharpbin</title>
<meta
name="description"
content="Create a new Sharpbin account to manage your pastes and settings."
/>
</svelte:head>
<main
class="flex min-h-[calc(100vh-120px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
>
<div class="w-full max-w-md rounded border-2 border-neutral-800 bg-neutral-900 p-6">
<h1 class="mb-6 text-center text-3xl font-bold">Register</h1>
{#if data.options?.registration_enabled === false}
<div
class="mb-4 rounded border border-yellow-600/80 bg-yellow-600/40 p-2 text-sm text-yellow-200"
>
Registration is currently disabled.
</div>
{/if}
<form on:submit|preventDefault={register} class="space-y-4">
<input
type="text"
placeholder="Username"
bind:value={username}
autocomplete="username"
required
disabled={loading || !data.options?.registration_enabled}
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
/>
<div>
<input
type="email"
placeholder="Email (optional)"
bind:value={email}
autocomplete="email"
disabled={loading || !data.options?.registration_enabled}
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
/>
</div>
<div>
<input
type="text"
placeholder="Display Name (optional)"
bind:value={displayName}
autocomplete="name"
disabled={loading || !data.options?.registration_enabled}
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
/>
</div>
<div class="flex w-full">
<input
type={showPassword ? 'text' : 'password'}
placeholder="Password"
bind:value={password}
required
on:focus={() => (passwordFocused = true)}
on:blur={() => (passwordFocused = false)}
autocomplete="new-password"
disabled={loading || !data.options?.registration_enabled}
class="focus:bg-neutral-750 w-[calc(100%-40px)] rounded-l border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
/>
<button
type="button"
on:click={() => (showPassword = !showPassword)}
disabled={loading || !data.options?.registration_enabled}
class="ml-2 flex w-10 items-center rounded-r border border-neutral-700 bg-neutral-800 p-2 transition-colors duration-200 hover:bg-neutral-700 focus:outline-none"
>
{#if showPassword}
<EyeClosed class="h-5 w-5 text-neutral-400" />
{:else}
<Eye class="h-5 w-5 text-neutral-400" />
{/if}
</button>
</div>
{#if passwordFocused}
<ul
transition:slide
class="space-y-2 rounded border border-neutral-700 bg-neutral-800 p-3 text-sm"
>
<li
class="flex items-center gap-2 {passwordChecks.minLength
? 'text-green-400'
: 'text-red-400'}"
>
{#if passwordChecks.minLength}
<Check class="h-4 w-4" />
{:else}
<X class="h-4 w-4" />
{/if}
At least 8 characters
</li>
<li
class="flex items-center gap-2 {passwordChecks.maxLength
? 'text-green-400'
: 'text-red-400'}"
>
{#if passwordChecks.maxLength}
<Check class="h-4 w-4" />
{:else}
<X class="h-4 w-4" />
{/if}
No more than 128 characters
</li>
<li
class="flex items-center gap-2 {passwordChecks.upper
? 'text-green-400'
: 'text-red-400'}"
>
{#if passwordChecks.upper}
<Check class="h-4 w-4" />
{:else}
<X class="h-4 w-4" />
{/if}
At least one uppercase letter
</li>
<li
class="flex items-center gap-2 {passwordChecks.lower
? 'text-green-400'
: 'text-red-400'}"
>
{#if passwordChecks.lower}
<Check class="h-4 w-4" />
{:else}
<X class="h-4 w-4" />
{/if}
At least one lowercase letter
</li>
<li
class="flex items-center gap-2 {passwordChecks.number
? 'text-green-400'
: 'text-red-400'}"
>
{#if passwordChecks.number}
<Check class="h-4 w-4" />
{:else}
<X class="h-4 w-4" />
{/if}
At least one number
</li>
</ul>
{/if}
<input
type="password"
placeholder="Confirm Password"
bind:value={confirmPassword}
required
autocomplete="new-password"
disabled={loading || !data.options?.registration_enabled}
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
/>
{#if confirmPassword && password !== confirmPassword}
<div
transition:fade
class="rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200"
>
Passwords do not match.
</div>
{/if}
{#if data.options?.cf_turnstile_site_key && data.options?.registration_enabled}
<div class="cf-turnstile w-full"></div>
{/if}
<button
type="submit"
disabled={!passwordValid ||
password !== confirmPassword ||
loading ||
!data.options?.registration_enabled}
class="w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900"
>
{loading ? 'Registering...' : 'Register'}
</button>
{#if error}
<div class="rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200">
{error}
</div>
{/if}
</form>
<p class="mt-4 text-center text-sm text-neutral-400">
Already have an account? <a
href={resolve('/login')}
class="text-neutral-200 transition-colors duration-200 hover:text-white">Login</a
>
</p>
</div>
</main>
<style>
*:disabled {
cursor: not-allowed;
background-color: var(--color-neutral-800);
color: var(--color-neutral-600) !important;
border: none;
}
*:disabled::placeholder {
color: var(--color-neutral-600) !important;
}
</style>