Files
shared/packages/web-ui/src/lib/components/AuthPage.svelte
T
Gabriel20xxandClaude Sonnet 5 56e2f60531 Make the auth screen's password field optional; catch up UserManagement drift
AuthPage's password (and, in setup mode, confirm-password) field no
longer forces a non-empty value client-side -- the server already
enforces the real rules (a correct password to log in, >=8 chars to
register/bootstrap), so this only replaces a native browser validation
popup with the server's own error message.

Also bring UserManagement in line with an AuthGuard-only edit that had
drifted ahead of this source (username/email/password relaxed to "at
least one of username/email, password optional" for admin-created
users) so the next consumer sync doesn't silently revert it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-09 16:52:45 +02:00

282 lines
8.3 KiB
Svelte

<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 = '',
faviconHref = '/favicon.svg',
loading = false,
onLogin,
onConfirmReset,
onRequestReset,
onSetup,
showTotp = true,
setupRequired = false,
tagline = ''
}: {
brand?: string;
children?: Snippet;
error?: string;
faviconHref?: string;
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('');
let totpRequired = $state(false);
let confirmPassword = $state('');
let submitError = $state('');
let submitting = $state(false);
let resetCode = $state('');
let resetError = $state('');
let resetOpen = $state(false);
let resetting = $state(false);
// A failed status fetch tells us nothing about whether setup is actually required —
// defaulting to the setup view in that case would wrongly invite creating a new
// account when the real problem is that the server couldn't be reached.
let effectiveSetupRequired = $derived(error ? false : setupRequired);
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'))
);
}
async function submit(event: SubmitEvent): Promise<void> {
event.preventDefault();
submitError = '';
if (effectiveSetupRequired && password !== confirmPassword) {
submitError = 'Passwords do not match.';
return;
}
submitting = true;
try {
if (effectiveSetupRequired) {
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.');
if (totpRequired && !totpToken.trim()) {
submitError = 'Enter the six-digit authenticator code to continue.';
return;
}
await onLogin({ identifier, password, totpToken: totpToken.trim() });
}
} catch (caught) {
if (!effectiveSetupRequired && showTotp && isTotpChallenge(caught)) {
totpRequired = true;
submitError = 'Enter the six-digit authenticator code to continue.';
} else {
if (!effectiveSetupRequired) {
totpRequired = false;
totpToken = '';
}
submitError = caught instanceof Error ? caught.message : 'Authentication failed.';
}
} 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-busy={loading} aria-labelledby="sc-auth-title" class="sc-auth-card">
<div class="sc-auth-brand">
<img alt="" aria-hidden="true" class="sc-auth-brand-icon" src={faviconHref} />
<span>{brand}</span>
</div>
{#if loading}
<h1 id="sc-auth-title">Loading...</h1>
{:else}
<h1 id="sc-auth-title">{effectiveSetupRequired ? 'Create your account' : 'Welcome back'}</h1>
{/if}
{#if !loading}
{#if tagline}
<p class="sc-auth-subtitle">{tagline}</p>
{/if}
<form class="sc-form sc-auth-form" onsubmit={submit}>
{#if effectiveSetupRequired}
<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={effectiveSetupRequired ? 'new-password' : 'current-password'}
class="sc-input"
minlength="8"
name="password"
type="password"
bind:value={password}
/>
</label>
{#if showTotp && !effectiveSetupRequired && totpRequired}
<label class="sc-field">
<span class="sc-label">Authenticator code</span>
<input
autocomplete="one-time-code"
class="sc-input"
inputmode="numeric"
name="totpToken"
type="text"
bind:value={totpToken}
/>
</label>
{/if}
{#if effectiveSetupRequired}
<label class="sc-field">
<span class="sc-label">Confirm password</span>
<input
autocomplete="new-password"
class="sc-input"
minlength="8"
name="confirmPassword"
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…' : effectiveSetupRequired ? 'Create account' : 'Log in'}
</Button>
</form>
{#if children}
<div class="sc-auth-extra">{@render children()}</div>
{/if}
{#if !effectiveSetupRequired && 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}
{/if}
</section>
</div>