2026-08-06 16:53:48 +02:00
|
|
|
<script lang="ts">
|
|
|
|
|
import type { Snippet } from 'svelte';
|
|
|
|
|
import type {
|
|
|
|
|
AuthLoginInput,
|
|
|
|
|
AuthResetConfirm,
|
|
|
|
|
AuthResetRequest,
|
|
|
|
|
AuthSetupInput
|
|
|
|
|
} from '../types';
|
|
|
|
|
import Button from './Button.svelte';
|
|
|
|
|
|
|
|
|
|
let {
|
|
|
|
|
brand = 'Application',
|
|
|
|
|
children,
|
|
|
|
|
error = '',
|
2026-08-09 10:34:06 +02:00
|
|
|
faviconHref = '/favicon.svg',
|
2026-08-06 16:53:48 +02:00
|
|
|
loading = false,
|
|
|
|
|
onLogin,
|
|
|
|
|
onConfirmReset,
|
|
|
|
|
onRequestReset,
|
|
|
|
|
onSetup,
|
|
|
|
|
showTotp = true,
|
|
|
|
|
setupRequired = false,
|
|
|
|
|
tagline = ''
|
|
|
|
|
}: {
|
|
|
|
|
brand?: string;
|
|
|
|
|
children?: Snippet;
|
|
|
|
|
error?: string;
|
2026-08-09 10:34:06 +02:00
|
|
|
faviconHref?: string;
|
2026-08-06 16:53:48 +02:00
|
|
|
loading?: boolean;
|
|
|
|
|
onLogin?: (input: AuthLoginInput) => void | Promise<void>;
|
|
|
|
|
onConfirmReset?: AuthResetConfirm;
|
|
|
|
|
onRequestReset?: AuthResetRequest;
|
|
|
|
|
onSetup?: (input: AuthSetupInput) => void | Promise<void>;
|
|
|
|
|
showTotp?: boolean;
|
|
|
|
|
setupRequired?: boolean;
|
|
|
|
|
tagline?: string;
|
|
|
|
|
} = $props();
|
|
|
|
|
|
|
|
|
|
let username = $state('');
|
|
|
|
|
let email = $state('');
|
|
|
|
|
let identifier = $state('');
|
|
|
|
|
let password = $state('');
|
|
|
|
|
let totpToken = $state('');
|
2026-08-07 15:22:26 +02:00
|
|
|
let totpRequired = $state(false);
|
2026-08-06 16:53:48 +02:00
|
|
|
let confirmPassword = $state('');
|
|
|
|
|
let submitError = $state('');
|
|
|
|
|
let submitting = $state(false);
|
|
|
|
|
let resetCode = $state('');
|
|
|
|
|
let resetError = $state('');
|
|
|
|
|
let resetOpen = $state(false);
|
|
|
|
|
let resetting = $state(false);
|
|
|
|
|
|
2026-08-07 15:22:26 +02:00
|
|
|
function isTotpChallenge(caught: unknown): boolean {
|
|
|
|
|
if (!(caught instanceof Error)) return false;
|
|
|
|
|
const message = caught.message.toLowerCase();
|
|
|
|
|
return (
|
|
|
|
|
message.includes('two-factor authentication code required') ||
|
|
|
|
|
message.includes('multi-factor authentication code required') ||
|
|
|
|
|
(message.includes('totp') && message.includes('required')) ||
|
|
|
|
|
(message.includes('mfa') && message.includes('required'))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-06 16:53:48 +02:00
|
|
|
async function submit(event: SubmitEvent): Promise<void> {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
submitError = '';
|
|
|
|
|
|
|
|
|
|
if (setupRequired && password !== confirmPassword) {
|
|
|
|
|
submitError = 'Passwords do not match.';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.');
|
2026-08-07 15:22:26 +02:00
|
|
|
if (totpRequired && !totpToken.trim()) {
|
|
|
|
|
submitError = 'Enter the six-digit authenticator code to continue.';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-06 16:53:48 +02:00
|
|
|
await onLogin({ identifier, password, totpToken: totpToken.trim() });
|
|
|
|
|
}
|
|
|
|
|
} catch (caught) {
|
2026-08-07 15:22:26 +02:00
|
|
|
if (!setupRequired && showTotp && isTotpChallenge(caught)) {
|
|
|
|
|
totpRequired = true;
|
|
|
|
|
submitError = 'Enter the six-digit authenticator code to continue.';
|
|
|
|
|
} else {
|
|
|
|
|
if (!setupRequired) {
|
|
|
|
|
totpRequired = false;
|
|
|
|
|
totpToken = '';
|
|
|
|
|
}
|
|
|
|
|
submitError = caught instanceof Error ? caught.message : 'Authentication failed.';
|
|
|
|
|
}
|
2026-08-06 16:53:48 +02:00
|
|
|
} finally {
|
|
|
|
|
submitting = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function requestReset(): Promise<void> {
|
|
|
|
|
resetError = '';
|
|
|
|
|
resetCode = '';
|
|
|
|
|
resetOpen = true;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!onRequestReset) throw new Error('The application has not configured its reset handler.');
|
|
|
|
|
await onRequestReset();
|
|
|
|
|
} catch (caught) {
|
|
|
|
|
resetError = caught instanceof Error ? caught.message : 'Unable to request an account reset.';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function confirmReset(): Promise<void> {
|
|
|
|
|
resetError = '';
|
|
|
|
|
const code = resetCode.trim();
|
|
|
|
|
if (!code) {
|
|
|
|
|
resetError = 'Enter the code printed in the server console.';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resetting = true;
|
|
|
|
|
try {
|
|
|
|
|
if (!onConfirmReset) throw new Error('The application has not configured its reset handler.');
|
|
|
|
|
await onConfirmReset(code);
|
|
|
|
|
} catch (caught) {
|
|
|
|
|
resetError = caught instanceof Error ? caught.message : 'Unable to reset the admin account.';
|
|
|
|
|
} finally {
|
|
|
|
|
resetting = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<div class="sc-root sc-auth-page">
|
|
|
|
|
<section aria-labelledby="sc-auth-title" class="sc-auth-card">
|
|
|
|
|
<div class="sc-auth-brand">
|
2026-08-09 10:34:06 +02:00
|
|
|
<img alt="" aria-hidden="true" class="sc-auth-brand-icon" src={faviconHref} />
|
2026-08-06 16:53:48 +02:00
|
|
|
<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>
|
|
|
|
|
|
2026-08-07 15:22:26 +02:00
|
|
|
{#if showTotp && !setupRequired && totpRequired}
|
2026-08-06 16:53:48 +02:00
|
|
|
<label class="sc-field">
|
2026-08-07 15:22:26 +02:00
|
|
|
<span class="sc-label">Authenticator code</span>
|
2026-08-06 16:53:48 +02:00
|
|
|
<input
|
|
|
|
|
autocomplete="one-time-code"
|
|
|
|
|
class="sc-input"
|
|
|
|
|
inputmode="numeric"
|
|
|
|
|
name="totpToken"
|
|
|
|
|
type="text"
|
|
|
|
|
bind:value={totpToken}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
{#if setupRequired}
|
|
|
|
|
<label class="sc-field">
|
|
|
|
|
<span class="sc-label">Confirm password</span>
|
|
|
|
|
<input
|
|
|
|
|
autocomplete="new-password"
|
|
|
|
|
class="sc-input"
|
|
|
|
|
minlength="8"
|
|
|
|
|
name="confirmPassword"
|
|
|
|
|
required
|
|
|
|
|
type="password"
|
|
|
|
|
bind:value={confirmPassword}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
{#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>
|
|
|
|
|
|
|
|
|
|
{#if children}
|
|
|
|
|
<div class="sc-auth-extra">{@render children()}</div>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
{#if !setupRequired && onRequestReset && onConfirmReset}
|
|
|
|
|
{#if resetOpen}
|
|
|
|
|
<div class="sc-auth-reset" aria-live="polite">
|
|
|
|
|
<p class="sc-auth-reset-hint">A reset code has been printed to the server console.</p>
|
|
|
|
|
<input
|
|
|
|
|
aria-label="Reset code"
|
|
|
|
|
autocomplete="off"
|
|
|
|
|
class="sc-input"
|
|
|
|
|
placeholder="Enter reset code"
|
|
|
|
|
type="text"
|
|
|
|
|
bind:value={resetCode}
|
|
|
|
|
/>
|
|
|
|
|
{#if resetError}<p class="sc-error">{resetError}</p>{/if}
|
|
|
|
|
<Button
|
|
|
|
|
disabled={resetting}
|
|
|
|
|
fullWidth
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="danger"
|
|
|
|
|
onclick={() => void confirmReset()}
|
|
|
|
|
>
|
|
|
|
|
{resetting ? 'Resetting…' : 'Confirm reset'}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
{:else}
|
|
|
|
|
<button class="sc-auth-reset-link" type="button" onclick={() => void requestReset()}>
|
|
|
|
|
Reset admin account
|
|
|
|
|
</button>
|
|
|
|
|
{/if}
|
|
|
|
|
{/if}
|
|
|
|
|
</section>
|
|
|
|
|
</div>
|