Files
minstrel/web/src/routes/register/+page.svelte
T
bvandeusenandClaude Opus 5.5 d99498be0b refactor(web): one password rule and one wording for account-form errors (DRY W4)
The 8-character minimum was written into sign-up, reset, Settings › Account
and Admin › Users as minlength="8" and a message, and each page mapped
password_too_short, username_invalid and username_taken to its own copy of
the same sentence. lib/auth/password.ts now holds the minimum, the hint and
"Passwords do not match.", and the pages take an error code's wording from
the shared error copy (knownCopy), falling back to their own.

username_invalid's shared copy now states the rule (3–32 characters:
letters, numbers, underscores, hyphens), as the pages did, on web and
Android alike. Account's mismatch line reads "Passwords do not match." like
the other forms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 03:14:50 -05:00

195 lines
7.0 KiB
Svelte

<script lang="ts">
import { PASSWORD_MIN_LENGTH, PASSWORDS_DIFFER } from '#lib/auth/password.js';
import { knownCopy } from '#lib/api/error-copy.js';
import { pageTitle } from '#lib/branding.js';
import { goto } from '$app/navigation';
import { onMount } from 'svelte';
import { getSetupStatus, register } from '#lib/auth/store.svelte.js';
import { errCode } from '#lib/api/errors.js';
import { rateLimitMessage } from '#lib/api/client.js';
import RestoreForm from '#lib/components/auth/RestoreForm.svelte';
let username = $state('');
let password = $state('');
let confirmPassword = $state('');
let inviteToken = $state('');
let setupToken = $state('');
// True on a brand-new server: the first account becomes admin and must
// carry the setup token from the server log.
let setupRequired = $state(false);
// A new server can instead start from a backup of another Minstrel.
let restoring = $state(false);
onMount(async () => {
try {
setupRequired = (await getSetupStatus()).setup_required;
} catch {
// Unknown: leave the ordinary form. If a token turns out to be needed,
// the server says so and the error below explains where to find it.
}
});
let displayName = $state('');
let submitting = $state(false);
let error = $state<string | null>(null);
function errorMessageFor(code: string): string {
switch (code) {
case 'invite_required':
return 'An invite token is needed to register. Ask an admin of this server for one.';
case 'invite_invalid':
return 'That invite token is invalid, expired, or already used.';
case 'setup_token_invalid':
return "That setup token doesn't match. Copy it from the server log; it changes each time the server restarts.";
default:
return knownCopy(code) ?? 'Registration failed. Please try again.';
}
}
async function handleSubmit(e: SubmitEvent) {
e.preventDefault();
if (submitting) return;
error = null;
if (password !== confirmPassword) {
error = PASSWORDS_DIFFER;
return;
}
submitting = true;
try {
await register({
username,
password,
inviteToken: inviteToken || undefined,
setupToken: setupToken.trim() || undefined,
displayName: displayName || undefined,
});
await goto('/', { replace: true });
} catch (err: unknown) {
error = rateLimitMessage(err) ?? errorMessageFor(errCode(err));
} finally {
submitting = false;
}
}
</script>
<svelte:head><title>{pageTitle('Register')}</title></svelte:head>
<main class="flex min-h-screen items-center justify-center bg-background text-text-primary">
<div class="w-full max-w-sm rounded-lg border border-border bg-surface p-6 shadow-sm">
<h1 class="mb-6 text-center text-2xl font-semibold">{restoring ? 'Restore a backup' : 'Create account'}</h1>
{#if setupRequired}
<p class="mb-4 text-center text-sm text-text-secondary">
{#if restoring}
<button type="button" class="text-accent-fg hover:underline" onclick={() => (restoring = false)}>
Create a new admin account instead
</button>
{:else}
Moving from another Minstrel?
<button type="button" class="text-accent-fg hover:underline" onclick={() => (restoring = true)}>
Restore a backup
</button>
{/if}
</p>
{/if}
{#if restoring}
<RestoreForm />
{:else}
<form class="space-y-4" onsubmit={handleSubmit}>
<label class="block">
<span class="mb-1 block text-sm text-text-secondary">Username</span>
<input
type="text"
autocomplete="username"
required
minlength="3"
maxlength="32"
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
bind:value={username}
/>
</label>
<label class="block">
<span class="mb-1 block text-sm text-text-secondary">
Display name <span class="text-text-secondary opacity-60">(optional)</span>
</span>
<input
type="text"
autocomplete="name"
maxlength="64"
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
bind:value={displayName}
/>
</label>
<label class="block">
<span class="mb-1 block text-sm text-text-secondary">Password</span>
<input
type="password"
autocomplete="new-password"
required
minlength={PASSWORD_MIN_LENGTH}
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
bind:value={password}
/>
</label>
<label class="block">
<span class="mb-1 block text-sm text-text-secondary">Confirm password</span>
<input
type="password"
autocomplete="new-password"
required
minlength={PASSWORD_MIN_LENGTH}
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
bind:value={confirmPassword}
/>
</label>
{#if setupRequired}
<label class="block">
<span class="mb-1 block text-sm text-text-secondary">Setup token</span>
<input
type="text"
autocomplete="off"
spellcheck="false"
required
class="w-full rounded border border-border bg-background px-3 py-2 font-mono outline-hidden focus:border-accent"
bind:value={setupToken}
/>
<span class="mt-1 block text-xs text-text-secondary">
This is a new server, so this account will be its admin. The setup token is printed in the
server log (look for "setup_token").
</span>
</label>
{:else}
<label class="block">
<span class="mb-1 block text-sm text-text-secondary">Invite token</span>
<input
type="text"
autocomplete="off"
spellcheck="false"
required
class="w-full rounded border border-border bg-background px-3 py-2 font-mono outline-hidden focus:border-accent"
bind:value={inviteToken}
/>
<span class="mt-1 block text-xs text-text-secondary">Ask an admin of this server for one.</span>
</label>
{/if}
<button
type="submit"
class="w-full rounded bg-accent px-3 py-2 font-medium text-background disabled:opacity-60"
aria-busy={submitting ? 'true' : undefined}
disabled={submitting}
>
{submitting ? 'Creating account…' : 'Create account'}
</button>
{#if error}
<p role="alert" class="text-sm text-danger">{error}</p>
{/if}
</form>
{/if}
<p class="mt-4 text-center text-sm text-text-secondary">
Already have an account?
<a href="/login" class="text-accent-fg hover:underline">Sign in</a>
</p>
</div>
</main>