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:
@@ -2,6 +2,12 @@ import errorCopyJson from '../styles/error-copy.json';
|
||||
|
||||
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 {
|
||||
if (!code) return ERROR_COPY.unknown ?? 'Something went wrong.';
|
||||
return ERROR_COPY[code] ?? ERROR_COPY.unknown ?? 'Something went wrong.';
|
||||
|
||||
@@ -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.';
|
||||
@@ -7,7 +7,7 @@
|
||||
"rate_limited": "Too many attempts. Wait a few minutes and try again.",
|
||||
"wrong_password": "Current password is incorrect.",
|
||||
"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.",
|
||||
"email_invalid": "Enter a valid email address.",
|
||||
"email_taken": "That email is already in use.",
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<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 { pageTitle } from '#lib/branding.js';
|
||||
import { useQueryClient } from '@tanstack/svelte-query';
|
||||
@@ -72,7 +74,7 @@
|
||||
async function onSubmitCreate(e: SubmitEvent) {
|
||||
e.preventDefault();
|
||||
if (createForm.password !== createForm.confirmPassword) {
|
||||
pushToast('Passwords do not match.', 'error');
|
||||
pushToast(PASSWORDS_DIFFER, 'error');
|
||||
return;
|
||||
}
|
||||
saving = true;
|
||||
@@ -95,12 +97,7 @@
|
||||
}
|
||||
|
||||
function createUserErrorMessage(code: string | undefined): string {
|
||||
switch (code) {
|
||||
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'}`;
|
||||
}
|
||||
return knownCopy(code) ?? `Create failed: ${code ?? 'unknown'}`;
|
||||
}
|
||||
|
||||
async function onConfirmDelete() {
|
||||
@@ -127,7 +124,7 @@
|
||||
e.preventDefault();
|
||||
if (!resetPasswordTarget) return;
|
||||
if (resetPasswordValue !== resetPasswordConfirm) {
|
||||
pushToast('Passwords do not match.', 'error');
|
||||
pushToast(PASSWORDS_DIFFER, 'error');
|
||||
return;
|
||||
}
|
||||
saving = true;
|
||||
@@ -139,11 +136,7 @@
|
||||
resetPasswordConfirm = '';
|
||||
} catch (e: unknown) {
|
||||
const code = errCode(e);
|
||||
if (code === 'password_too_short') {
|
||||
pushToast('Password must be at least 8 characters.', 'error');
|
||||
} else {
|
||||
pushToast(`Reset failed: ${code}`, 'error');
|
||||
}
|
||||
pushToast(knownCopy(code) ?? `Reset failed: ${code}`, 'error');
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
@@ -399,7 +392,7 @@
|
||||
id="cu-password"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
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"
|
||||
/>
|
||||
@@ -410,7 +403,7 @@
|
||||
id="cu-confirm"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
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"
|
||||
/>
|
||||
@@ -454,7 +447,7 @@
|
||||
id="rp-password"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
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"
|
||||
/>
|
||||
@@ -465,7 +458,7 @@
|
||||
id="rp-confirm"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
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"
|
||||
/>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<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';
|
||||
@@ -32,20 +34,14 @@
|
||||
|
||||
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 '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 '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.';
|
||||
return knownCopy(code) ?? 'Registration failed. Please try again.';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +50,7 @@
|
||||
if (submitting) return;
|
||||
error = null;
|
||||
if (password !== confirmPassword) {
|
||||
error = 'Passwords do not match.';
|
||||
error = PASSWORDS_DIFFER;
|
||||
return;
|
||||
}
|
||||
submitting = true;
|
||||
@@ -128,7 +124,7 @@
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
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"
|
||||
bind:value={password}
|
||||
/>
|
||||
@@ -139,7 +135,7 @@
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
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"
|
||||
bind:value={confirmPassword}
|
||||
/>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<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 { page } from '$app/state';
|
||||
import { pageTitle } from '#lib/branding.js';
|
||||
@@ -21,7 +23,7 @@
|
||||
return;
|
||||
}
|
||||
if (newPassword !== confirmPassword) {
|
||||
error = 'Passwords do not match.';
|
||||
error = PASSWORDS_DIFFER;
|
||||
return;
|
||||
}
|
||||
submitting = true;
|
||||
@@ -35,10 +37,8 @@
|
||||
error = throttled;
|
||||
} else if (code === 'invalid_token') {
|
||||
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 {
|
||||
error = `Reset failed: ${code}`;
|
||||
error = knownCopy(code) ?? `Reset failed: ${code}`;
|
||||
}
|
||||
} finally {
|
||||
submitting = false;
|
||||
@@ -58,7 +58,7 @@
|
||||
id="new-password"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
autocomplete="new-password"
|
||||
bind:value={newPassword}
|
||||
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"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
autocomplete="new-password"
|
||||
bind:value={confirmPassword}
|
||||
class="w-full rounded border border-border bg-background px-3 py-2 outline-hidden focus:border-accent"
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<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 { onMount } from 'svelte';
|
||||
import { useQueryClient } from '@tanstack/svelte-query';
|
||||
@@ -84,7 +86,7 @@
|
||||
async function onSavePassword(e: SubmitEvent) {
|
||||
e.preventDefault();
|
||||
if (next !== confirm) {
|
||||
pushToast('New passwords do not match.', 'error');
|
||||
pushToast(PASSWORDS_DIFFER, 'error');
|
||||
return;
|
||||
}
|
||||
passwordSaving = true;
|
||||
@@ -99,9 +101,7 @@
|
||||
signOutSubsonic = false;
|
||||
} catch (err: unknown) {
|
||||
const code = errCode(err);
|
||||
if (code === 'wrong_password') pushToast('Current password is incorrect.', 'error');
|
||||
else if (code === 'password_too_short') pushToast('Password must be at least 8 characters.', 'error');
|
||||
else pushToast(`Change failed: ${code}`, 'error');
|
||||
pushToast(knownCopy(code) ?? `Change failed: ${code}`, 'error');
|
||||
} finally {
|
||||
passwordSaving = false;
|
||||
}
|
||||
@@ -161,18 +161,18 @@
|
||||
id="password-current"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
autocomplete="current-password"
|
||||
bind:value={current}
|
||||
class={input}
|
||||
/>
|
||||
</SettingRow>
|
||||
<SettingRow label="New password" hint="At least 8 characters." for="password-new">
|
||||
<SettingRow label="New password" hint={PASSWORD_HINT} for="password-new">
|
||||
<input
|
||||
id="password-new"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
autocomplete="new-password"
|
||||
bind:value={next}
|
||||
class={input}
|
||||
@@ -183,7 +183,7 @@
|
||||
id="password-confirm"
|
||||
type="password"
|
||||
required
|
||||
minlength="8"
|
||||
minlength={PASSWORD_MIN_LENGTH}
|
||||
autocomplete="new-password"
|
||||
bind:value={confirm}
|
||||
class={input}
|
||||
|
||||
@@ -68,7 +68,7 @@ describe('Settings › Account', () => {
|
||||
await fireEvent.input(screen.getByLabelText(/confirm new password/i), { target: { value: 'different1' } });
|
||||
await fireEvent.click(screen.getByRole('button', { name: /change password/i }));
|
||||
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 () => {
|
||||
|
||||
Reference in New Issue
Block a user