Files
minstrel/web/src/routes/register/+page.svelte
T
bvandeusenandClaude Opus 5.5 29042e4eb2
release / govulncheck (push) Successful in 29s
release / web (push) Failing after 1m21s
release / go (push) Successful in 1m39s
release / integration (push) Successful in 5m26s
release / android (push) Successful in 5m27s
release / Build signed APK (push) Successful in 5m44s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
feat: a new Minstrel restores a backup, from the web app or the command line, then rescans (#6775)
A new server (no accounts, no library) offers "Restore a backup" beside
"Create account". The file goes up as the request body to
POST /api/auth/restore with the setup token from the log in X-Setup-Token,
since the backup's admins become this server's. It loads in one transaction,
then a library scan starts and finds each file at its path. The same load runs
offline as `minstrel restore FILE`, which migrates the schema first.

That path alone gets a 1 GiB, 15-minute body allowance instead of 4 MiB and
30s, and skips the JSON-only check for cookie writes: a stale cookie from the
old server would otherwise refuse the upload, and the custom header already
rules out a cross-site form.

The library sync answers 410 cursor_ahead to a cursor past the last one the
server has handed out, read from the change sequence, so an app synced with
the old server after the backup was made starts over instead of skipping the
ids the restored server reuses. Restore now leaves each sequence as having
issued the restored maximum, so that reading holds; a compacted log still
counts its handed-out cursors.

backup.Read keeps the underlying error in its chain, so an oversized upload
answers 413 rather than "not a backup". The hosting docs say how to put the
music back first and what is entered again afterwards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 16:10:06 -05:00

198 lines
7.0 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
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 'username_invalid':
return 'Username must be 3–32 characters: letters, numbers, underscores, hyphens.';
case 'password_too_short':
return 'Password must be at least 8 characters.';
case 'invite_required':
return 'This server requires an invite token to register. Ask an admin for one.';
case 'invite_invalid':
return 'That invite token is invalid, expired, or already used.';
case 'username_taken':
return 'That username is already taken.';
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 'Registration failed. Please try again.';
}
}
async function handleSubmit(e: SubmitEvent) {
e.preventDefault();
if (submitting) return;
error = null;
if (password !== confirmPassword) {
error = 'Passwords do not match.';
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="8"
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="8"
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 class="text-text-secondary opacity-60">(if required by your server)</span>
</span>
<input
type="text"
autocomplete="off"
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
bind:value={inviteToken}
/>
</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>