Sync shared snapshot from df68167
This commit is contained in:
1 parent
588ef139a6
commit
06efc832ca
79 files changed
+10035
-3
No files matched your search
@@ -0,0 +1,245 @@
|
||||
<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 = '',
|
||||
loading = false,
|
||||
onLogin,
|
||||
onConfirmReset,
|
||||
onRequestReset,
|
||||
onSetup,
|
||||
showTotp = true,
|
||||
setupRequired = false,
|
||||
tagline = ''
|
||||
}: {
|
||||
brand?: string;
|
||||
children?: Snippet;
|
||||
error?: 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 confirmPassword = $state('');
|
||||
let submitError = $state('');
|
||||
let submitting = $state(false);
|
||||
let resetCode = $state('');
|
||||
let resetError = $state('');
|
||||
let resetOpen = $state(false);
|
||||
let resetting = $state(false);
|
||||
|
||||
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.');
|
||||
await onLogin({ identifier, password, totpToken: totpToken.trim() });
|
||||
}
|
||||
} catch (caught) {
|
||||
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-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 showTotp && !setupRequired}
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Authenticator code <span class="sc-label-optional">(if enabled)</span></span>
|
||||
<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>
|
||||
Reference in new issue
Block a user