feat(auth): password rules checked while typing, with the server's own policy

The password policy moves to @trek/shared, where the server keeps enforcing it unchanged and every form that sets a password now runs it before sending: the forced change after the first login, registration, the reset link, the account settings on desktop and phone, and creating or editing a user in admin. Under each new password a checklist ticks off length, upper and lower case, a number and a special character, and a refusal names the actual reason, a too common or too repetitive password included. (#1200)
This commit is contained in:
Maurice
2026-10-01 09:59:20 +02:00
parent 44cc596c39
commit 542a57b74e
46 changed files with 546 additions and 72 deletions
@@ -0,0 +1,37 @@
// FE-SHARED-PWCHECK-001 to FE-SHARED-PWCHECK-004
import { describe, expect, it } from 'vitest'
import { render, screen } from '../../../tests/helpers/render'
import PasswordChecklist from './PasswordChecklist'
import { passwordErrorKey } from '../../utils/passwordError'
describe('PasswordChecklist (#1200)', () => {
it('FE-SHARED-PWCHECK-001: stays out of the way until something is typed', () => {
const { container } = render(<PasswordChecklist password="" />)
expect(container).toBeEmptyDOMElement()
})
it('FE-SHARED-PWCHECK-002: ticks off each rule the password meets', () => {
render(<PasswordChecklist password="abcdefg1" />)
const list = screen.getByRole('list', { name: 'Password requirements' })
const met = Array.from(list.querySelectorAll('li[data-met]')).map(li => li.textContent)
expect(met).toEqual(['At least 8 characters', 'A lowercase letter', 'A number'])
expect(list.querySelectorAll('li')).toHaveLength(5)
})
it('FE-SHARED-PWCHECK-003: the always-light sign-in card keeps fixed colours', () => {
render(<PasswordChecklist password="A" tone="light" />)
const upper = screen.getByText('An uppercase letter').closest('li') as HTMLElement
expect(upper.getAttribute('data-met')).toBe('true')
expect(upper.style.background).not.toBe('')
})
})
describe('passwordErrorKey (#1200)', () => {
it('FE-SHARED-PWCHECK-004: names the problem the server would answer with, or nothing', () => {
expect(passwordErrorKey('Ab1!')).toBe('settings.passwordTooShort')
expect(passwordErrorKey('zzzzzzzzzz')).toBe('settings.passwordRepetitive')
expect(passwordErrorKey('Password')).toBe('settings.passwordCommon')
expect(passwordErrorKey('longenough1')).toBe('settings.passwordWeak')
expect(passwordErrorKey('Longenough1!')).toBeNull()
})
})
@@ -0,0 +1,33 @@
import { Check, Circle } from 'lucide-react'
import { PASSWORD_RULES, passwordRuleStates } from '@trek/shared'
import { useTranslation } from '../../i18n'
// The sign-in and reset pages are always light, whatever the theme, so their
// list keeps fixed colours instead of the theme tokens.
const LIGHT_MET = { background: '#dcfce7', color: '#15803d' } // theme-lint-disable: the always-light sign-in card
const LIGHT_OPEN = { background: '#f3f4f6', color: '#6b7280' } // theme-lint-disable: the always-light sign-in card
/**
* The rules under a new-password field, each ticked off as the password meets
* it (#1200). Hidden until something is typed, so an empty form stays calm.
*/
export default function PasswordChecklist({ password, className = '', tone = 'theme' }: { password: string; className?: string; tone?: 'theme' | 'light' }) {
const { t } = useTranslation()
if (!password) return null
const states = passwordRuleStates(password)
return (
<ul aria-label={t('settings.passwordRules')} className={`flex flex-wrap gap-1.5 ${className}`}>
{PASSWORD_RULES.map(rule => {
const met = states[rule]
return (
<li key={rule} data-met={met || undefined}
className={`inline-flex items-center gap-1 rounded-full px-2 py-[3px] text-caption font-medium ${tone === 'light' ? '' : met ? 'bg-success-soft text-success' : 'bg-surface-tertiary text-content-faint'}`}
style={tone === 'light' ? (met ? LIGHT_MET : LIGHT_OPEN) : undefined}>
{met ? <Check size={11} strokeWidth={2.6} aria-hidden /> : <Circle size={9} strokeWidth={2.4} aria-hidden />}
<span>{t(`settings.passwordRule.${rule}`)}</span>
</li>
)
})}
</ul>
)
}