124 lines
3.4 KiB
Svelte
124 lines
3.4 KiB
Svelte
<script lang="ts">
|
|||
|
|
import type { AuthLoginInput, AuthSetupInput } from '../types';
|
||
|
|
import Button from './Button.svelte';
|
||
|
|
|
||
|
|
let {
|
||
|
|
brand = 'Application',
|
||
|
|
error = '',
|
||
|
|
loading = false,
|
||
|
|
onLogin,
|
||
|
|
onSetup,
|
||
|
|
setupRequired = false,
|
||
|
|
tagline = ''
|
||
|
|
}: {
|
||
|
|
brand?: string;
|
||
|
|
error?: string;
|
||
|
|
loading?: boolean;
|
||
|
|
onLogin?: (input: AuthLoginInput) => void | Promise<void>;
|
||
|
|
onSetup?: (input: AuthSetupInput) => void | Promise<void>;
|
||
|
|
setupRequired?: boolean;
|
||
|
|
tagline?: string;
|
||
|
|
} = $props();
|
||
|
|
|
||
|
|
let username = $state('');
|
||
|
|
let email = $state('');
|
||
|
|
let identifier = $state('');
|
||
|
|
let password = $state('');
|
||
|
|
let submitError = $state('');
|
||
|
|
let submitting = $state(false);
|
||
|
|
|
||
|
|
async function submit(event: SubmitEvent): Promise<void> {
|
||
|
|
event.preventDefault();
|
||
|
|
submitError = '';
|
||
|
|
submitting = true;
|
||
|
|
|
||
|
|
try {
|
||
|
|
if (setupRequired) {
|
||
|
|
if (!onSetup) throw new Error('The application has not configured its setup handler.');
|
||
|
|
await onSetup({ username, email, password });
|
||
|
|
} else {
|
||
|
|
if (!onLogin) throw new Error('The application has not configured its login handler.');
|
||
|
|
await onLogin({ identifier, password });
|
||
|
|
}
|
||
|
|
} catch (caught) {
|
||
|
|
submitError = caught instanceof Error ? caught.message : 'Authentication failed.';
|
||
|
|
} finally {
|
||
|
|
submitting = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<div class="sc-root sc-auth-page">
|
||
|
|
<section aria-labelledby="sc-auth-title" class="sc-auth-card">
|
||
|
|
<div class="sc-auth-brand">
|
||
|
|
<span aria-hidden="true" class="sc-brand-mark">{brand.slice(0, 1).toUpperCase()}</span>
|
||
|
|
<span>{brand}</span>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<h1 id="sc-auth-title">{setupRequired ? 'Create your account' : 'Welcome back'}</h1>
|
||
|
|
{#if tagline}
|
||
|
|
<p class="sc-auth-subtitle">{tagline}</p>
|
||
|
|
{/if}
|
||
|
|
|
||
|
|
<form class="sc-form sc-auth-form" onsubmit={submit}>
|
||
|
|
{#if setupRequired}
|
||
|
|
<label class="sc-field">
|
||
|
|
<span class="sc-label">Username</span>
|
||
|
|
<input
|
||
|
|
autocomplete="username"
|
||
|
|
class="sc-input"
|
||
|
|
name="username"
|
||
|
|
required
|
||
|
|
type="text"
|
||
|
|
bind:value={username}
|
||
|
|
/>
|
||
|
|
</label>
|
||
|
|
|
||
|
|
<label class="sc-field">
|
||
|
|
<span class="sc-label">Email</span>
|
||
|
|
<input
|
||
|
|
autocomplete="email"
|
||
|
|
class="sc-input"
|
||
|
|
name="email"
|
||
|
|
required
|
||
|
|
type="email"
|
||
|
|
bind:value={email}
|
||
|
|
/>
|
||
|
|
</label>
|
||
|
|
{:else}
|
||
|
|
<label class="sc-field">
|
||
|
|
<span class="sc-label">Username or email</span>
|
||
|
|
<input
|
||
|
|
autocomplete="username"
|
||
|
|
class="sc-input"
|
||
|
|
name="identifier"
|
||
|
|
required
|
||
|
|
type="text"
|
||
|
|
bind:value={identifier}
|
||
|
|
/>
|
||
|
|
</label>
|
||
|
|
{/if}
|
||
|
|
|
||
|
|
<label class="sc-field">
|
||
|
|
<span class="sc-label">Password</span>
|
||
|
|
<input
|
||
|
|
autocomplete={setupRequired ? 'new-password' : 'current-password'}
|
||
|
|
class="sc-input"
|
||
|
|
minlength="8"
|
||
|
|
name="password"
|
||
|
|
required
|
||
|
|
type="password"
|
||
|
|
bind:value={password}
|
||
|
|
/>
|
||
|
|
</label>
|
||
|
|
|
||
|
|
{#if error || submitError}
|
||
|
|
<p aria-live="polite" class="sc-error">{submitError || error}</p>
|
||
|
|
{/if}
|
||
|
|
|
||
|
|
<Button disabled={loading || submitting} fullWidth size="lg" type="submit">
|
||
|
|
{loading || submitting ? 'Please wait…' : setupRequired ? 'Create account' : 'Log in'}
|
||
|
|
</Button>
|
||
|
|
</form>
|
||
|
|
</section>
|
||
|
|
</div>
|