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>
This commit is contained in:
2026-11-27 03:14:50 -05:00
co-authored by Claude Opus 5.5
parent 87c650aca1
commit d99498be0b
9 changed files with 50 additions and 44 deletions
@@ -79,7 +79,7 @@ object ErrorCopy {
"rate_limited" to "Too many attempts. Wait a few minutes and try again.", "rate_limited" to "Too many attempts. Wait a few minutes and try again.",
"wrong_password" to "Current password is incorrect.", "wrong_password" to "Current password is incorrect.",
"password_too_short" to "Password must be at least 8 characters.", "password_too_short" to "Password must be at least 8 characters.",
"username_invalid" to "That username isn't valid.", "username_invalid" to "Username must be 3–32 characters: letters, numbers, underscores, hyphens.",
"username_taken" to "That username is already taken.", "username_taken" to "That username is already taken.",
"email_invalid" to "Enter a valid email address.", "email_invalid" to "Enter a valid email address.",
"email_taken" to "That email is already in use.", "email_taken" to "That email is already in use.",
+6
View File
@@ -2,6 +2,12 @@ import errorCopyJson from '../styles/error-copy.json';
export const ERROR_COPY: Readonly<Record<string, string>> = errorCopyJson; export const ERROR_COPY: Readonly<Record<string, string>> = errorCopyJson;
/** The fixed copy for a known error code, or undefined for any other code,
so a form can fall back to its own wording. */
export function knownCopy(code: string | null | undefined): string | undefined {
return code && code !== 'unknown' ? ERROR_COPY[code] : undefined;
}
export function copyForCode(code: string | null | undefined): string { export function copyForCode(code: string | null | undefined): string {
if (!code) return ERROR_COPY.unknown ?? 'Something went wrong.'; if (!code) return ERROR_COPY.unknown ?? 'Something went wrong.';
return ERROR_COPY[code] ?? ERROR_COPY.unknown ?? 'Something went wrong.'; return ERROR_COPY[code] ?? ERROR_COPY.unknown ?? 'Something went wrong.';
+11
View File
@@ -0,0 +1,11 @@
// The account-form rules every password field shares (sign-up, reset,
// Settings › Account, Admin › Users). The server enforces the same minimum
// (password_too_short); the field's minlength only saves a round trip.
export const PASSWORD_MIN_LENGTH = 8;
/** The hint under a new-password field. */
export const PASSWORD_HINT = `At least ${PASSWORD_MIN_LENGTH} characters.`;
/** Said when a password and its confirmation differ. */
export const PASSWORDS_DIFFER = 'Passwords do not match.';
+1 -1
View File
@@ -7,7 +7,7 @@
"rate_limited": "Too many attempts. Wait a few minutes and try again.", "rate_limited": "Too many attempts. Wait a few minutes and try again.",
"wrong_password": "Current password is incorrect.", "wrong_password": "Current password is incorrect.",
"password_too_short": "Password must be at least 8 characters.", "password_too_short": "Password must be at least 8 characters.",
"username_invalid": "That username isn't valid.", "username_invalid": "Username must be 3–32 characters: letters, numbers, underscores, hyphens.",
"username_taken": "That username is already taken.", "username_taken": "That username is already taken.",
"email_invalid": "Enter a valid email address.", "email_invalid": "Enter a valid email address.",
"email_taken": "That email is already in use.", "email_taken": "That email is already in use.",
+10 -17
View File
@@ -1,4 +1,6 @@
<script lang="ts"> <script lang="ts">
import { PASSWORD_MIN_LENGTH, PASSWORDS_DIFFER } from '#lib/auth/password.js';
import { knownCopy } from '#lib/api/error-copy.js';
import { plural } from '#lib/utils/plural.js'; import { plural } from '#lib/utils/plural.js';
import { pageTitle } from '#lib/branding.js'; import { pageTitle } from '#lib/branding.js';
import { useQueryClient } from '@tanstack/svelte-query'; import { useQueryClient } from '@tanstack/svelte-query';
@@ -72,7 +74,7 @@
async function onSubmitCreate(e: SubmitEvent) { async function onSubmitCreate(e: SubmitEvent) {
e.preventDefault(); e.preventDefault();
if (createForm.password !== createForm.confirmPassword) { if (createForm.password !== createForm.confirmPassword) {
pushToast('Passwords do not match.', 'error'); pushToast(PASSWORDS_DIFFER, 'error');
return; return;
} }
saving = true; saving = true;
@@ -95,12 +97,7 @@
} }
function createUserErrorMessage(code: string | undefined): string { function createUserErrorMessage(code: string | undefined): string {
switch (code) { return knownCopy(code) ?? `Create failed: ${code ?? 'unknown'}`;
case 'username_taken': return 'That username is already taken.';
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.';
default: return `Create failed: ${code ?? 'unknown'}`;
}
} }
async function onConfirmDelete() { async function onConfirmDelete() {
@@ -127,7 +124,7 @@
e.preventDefault(); e.preventDefault();
if (!resetPasswordTarget) return; if (!resetPasswordTarget) return;
if (resetPasswordValue !== resetPasswordConfirm) { if (resetPasswordValue !== resetPasswordConfirm) {
pushToast('Passwords do not match.', 'error'); pushToast(PASSWORDS_DIFFER, 'error');
return; return;
} }
saving = true; saving = true;
@@ -139,11 +136,7 @@
resetPasswordConfirm = ''; resetPasswordConfirm = '';
} catch (e: unknown) { } catch (e: unknown) {
const code = errCode(e); const code = errCode(e);
if (code === 'password_too_short') { pushToast(knownCopy(code) ?? `Reset failed: ${code}`, 'error');
pushToast('Password must be at least 8 characters.', 'error');
} else {
pushToast(`Reset failed: ${code}`, 'error');
}
} finally { } finally {
saving = false; saving = false;
} }
@@ -399,7 +392,7 @@
id="cu-password" id="cu-password"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
bind:value={createForm.password} bind:value={createForm.password}
class="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:outline-hidden focus:ring-2 focus:ring-accent" class="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:outline-hidden focus:ring-2 focus:ring-accent"
/> />
@@ -410,7 +403,7 @@
id="cu-confirm" id="cu-confirm"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
bind:value={createForm.confirmPassword} bind:value={createForm.confirmPassword}
class="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:outline-hidden focus:ring-2 focus:ring-accent" class="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:outline-hidden focus:ring-2 focus:ring-accent"
/> />
@@ -454,7 +447,7 @@
id="rp-password" id="rp-password"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
bind:value={resetPasswordValue} bind:value={resetPasswordValue}
class="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:outline-hidden focus:ring-2 focus:ring-accent" class="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:outline-hidden focus:ring-2 focus:ring-accent"
/> />
@@ -465,7 +458,7 @@
id="rp-confirm" id="rp-confirm"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
bind:value={resetPasswordConfirm} bind:value={resetPasswordConfirm}
class="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:outline-hidden focus:ring-2 focus:ring-accent" class="mt-1 w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:outline-hidden focus:ring-2 focus:ring-accent"
/> />
+6 -10
View File
@@ -1,4 +1,6 @@
<script lang="ts"> <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 { pageTitle } from '#lib/branding.js';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
@@ -32,20 +34,14 @@
function errorMessageFor(code: string): string { function errorMessageFor(code: string): string {
switch (code) { 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': case 'invite_required':
return 'An invite token is needed to register. Ask an admin of this server for one.'; return 'An invite token is needed to register. Ask an admin of this server for one.';
case 'invite_invalid': case 'invite_invalid':
return 'That invite token is invalid, expired, or already used.'; return 'That invite token is invalid, expired, or already used.';
case 'username_taken':
return 'That username is already taken.';
case 'setup_token_invalid': case 'setup_token_invalid':
return "That setup token doesn't match. Copy it from the server log; it changes each time the server restarts."; return "That setup token doesn't match. Copy it from the server log; it changes each time the server restarts.";
default: default:
return 'Registration failed. Please try again.'; return knownCopy(code) ?? 'Registration failed. Please try again.';
} }
} }
@@ -54,7 +50,7 @@
if (submitting) return; if (submitting) return;
error = null; error = null;
if (password !== confirmPassword) { if (password !== confirmPassword) {
error = 'Passwords do not match.'; error = PASSWORDS_DIFFER;
return; return;
} }
submitting = true; submitting = true;
@@ -128,7 +124,7 @@
type="password" type="password"
autocomplete="new-password" autocomplete="new-password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent" class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
bind:value={password} bind:value={password}
/> />
@@ -139,7 +135,7 @@
type="password" type="password"
autocomplete="new-password" autocomplete="new-password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent" class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
bind:value={confirmPassword} bind:value={confirmPassword}
/> />
@@ -1,4 +1,6 @@
<script lang="ts"> <script lang="ts">
import { PASSWORD_MIN_LENGTH, PASSWORDS_DIFFER } from '#lib/auth/password.js';
import { knownCopy } from '#lib/api/error-copy.js';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { page } from '$app/state'; import { page } from '$app/state';
import { pageTitle } from '#lib/branding.js'; import { pageTitle } from '#lib/branding.js';
@@ -21,7 +23,7 @@
return; return;
} }
if (newPassword !== confirmPassword) { if (newPassword !== confirmPassword) {
error = 'Passwords do not match.'; error = PASSWORDS_DIFFER;
return; return;
} }
submitting = true; submitting = true;
@@ -35,10 +37,8 @@
error = throttled; error = throttled;
} else if (code === 'invalid_token') { } else if (code === 'invalid_token') {
error = 'This reset link is invalid, expired, or already used. Request a new one.'; error = 'This reset link is invalid, expired, or already used. Request a new one.';
} else if (code === 'password_too_short') {
error = 'Password must be at least 8 characters.';
} else { } else {
error = `Reset failed: ${code}`; error = knownCopy(code) ?? `Reset failed: ${code}`;
} }
} finally { } finally {
submitting = false; submitting = false;
@@ -58,7 +58,7 @@
id="new-password" id="new-password"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
autocomplete="new-password" autocomplete="new-password"
bind:value={newPassword} bind:value={newPassword}
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent" class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
@@ -70,7 +70,7 @@
id="confirm-password" id="confirm-password"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
autocomplete="new-password" autocomplete="new-password"
bind:value={confirmPassword} bind:value={confirmPassword}
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent" class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
+8 -8
View File
@@ -1,4 +1,6 @@
<script lang="ts"> <script lang="ts">
import { PASSWORD_HINT, PASSWORD_MIN_LENGTH, PASSWORDS_DIFFER } from '#lib/auth/password.js';
import { knownCopy } from '#lib/api/error-copy.js';
import SettingsColumns from '#lib/components/settings/SettingsColumns.svelte'; import SettingsColumns from '#lib/components/settings/SettingsColumns.svelte';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { useQueryClient } from '@tanstack/svelte-query'; import { useQueryClient } from '@tanstack/svelte-query';
@@ -84,7 +86,7 @@
async function onSavePassword(e: SubmitEvent) { async function onSavePassword(e: SubmitEvent) {
e.preventDefault(); e.preventDefault();
if (next !== confirm) { if (next !== confirm) {
pushToast('New passwords do not match.', 'error'); pushToast(PASSWORDS_DIFFER, 'error');
return; return;
} }
passwordSaving = true; passwordSaving = true;
@@ -99,9 +101,7 @@
signOutSubsonic = false; signOutSubsonic = false;
} catch (err: unknown) { } catch (err: unknown) {
const code = errCode(err); const code = errCode(err);
if (code === 'wrong_password') pushToast('Current password is incorrect.', 'error'); pushToast(knownCopy(code) ?? `Change failed: ${code}`, 'error');
else if (code === 'password_too_short') pushToast('Password must be at least 8 characters.', 'error');
else pushToast(`Change failed: ${code}`, 'error');
} finally { } finally {
passwordSaving = false; passwordSaving = false;
} }
@@ -161,18 +161,18 @@
id="password-current" id="password-current"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
autocomplete="current-password" autocomplete="current-password"
bind:value={current} bind:value={current}
class={input} class={input}
/> />
</SettingRow> </SettingRow>
<SettingRow label="New password" hint="At least 8 characters." for="password-new"> <SettingRow label="New password" hint={PASSWORD_HINT} for="password-new">
<input <input
id="password-new" id="password-new"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
autocomplete="new-password" autocomplete="new-password"
bind:value={next} bind:value={next}
class={input} class={input}
@@ -183,7 +183,7 @@
id="password-confirm" id="password-confirm"
type="password" type="password"
required required
minlength="8" minlength={PASSWORD_MIN_LENGTH}
autocomplete="new-password" autocomplete="new-password"
bind:value={confirm} bind:value={confirm}
class={input} class={input}
@@ -68,7 +68,7 @@ describe('Settings › Account', () => {
await fireEvent.input(screen.getByLabelText(/confirm new password/i), { target: { value: 'different1' } }); await fireEvent.input(screen.getByLabelText(/confirm new password/i), { target: { value: 'different1' } });
await fireEvent.click(screen.getByRole('button', { name: /change password/i })); await fireEvent.click(screen.getByRole('button', { name: /change password/i }));
expect(changePassword).not.toHaveBeenCalled(); expect(changePassword).not.toHaveBeenCalled();
expect(pushToast).toHaveBeenCalledWith('New passwords do not match.', 'error'); expect(pushToast).toHaveBeenCalledWith('Passwords do not match.', 'error');
}); });
test('matched passwords call changePassword(current, new)', async () => { test('matched passwords call changePassword(current, new)', async () => {