Platform-v2 step 2.6b:
- backend: POST /api/auth/unlock {pin|password} checks the CURRENT session's
user and keeps the session (no new login/token); 5 wrong tries end it
(auth.wrong_pin with attempts_left, then auth.session_ended); bootstrap
gains `login` (manager_mode, autofill_username, support)
- manager LoginPage rewritten on @xenia/ui + i18n: client "manager", password
or PIN pad per setting, remembered/only account card, support access panel
(challenge -> code -> 4 h session, not remembered as an account), notice
when the license runs the free tier (expired/inactive/locked/clock/unverified)
- lock screen on /api/auth/unlock with the shared PinPad
- @xenia/ui PinPad: variable length (minLength/maxLength, OK key, Enter)
- e2e manager.mjs: 33 checks (+ lockout after 5 wrong PINs, PIN login mode,
support access with a wrong then right code)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
6.1 KiB
React
142 lines
6.1 KiB
React
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
|
||
import { cx } from '../internal/cx.js'
|
||
import { Spinner } from './Spinner.jsx'
|
||
import { canSubmit, nextPin, pinAction } from './pin.js'
|
||
import { useLabels } from './labels.js'
|
||
|
||
const KEYS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', null, '0', 'Backspace']
|
||
const KEYS_VARIABLE = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'Enter', '0', 'Backspace']
|
||
|
||
/**
|
||
* Numeric PIN entry: dots + a 3×4 keypad, also driven by the physical keyboard.
|
||
*
|
||
* <PinPad length={4} onComplete={async pin => { await unlock(pin) }} />
|
||
*
|
||
* Fixed length (`length`, default 4): onComplete(pin) runs as soon as `length` digits are in.
|
||
* Variable length (`minLength` + `maxLength`, e.g. PINs of 4–8 digits): an OK key (and the
|
||
* Enter key) submits once at least `minLength` digits are in.
|
||
*
|
||
* If onComplete returns a promise the pad
|
||
* shows a spinner until it settles. If it throws / rejects (or returns false), the pad
|
||
* shakes, shows `error` text if given, and clears for another try. On success it clears too.
|
||
*
|
||
* error: text under the dots (e.g. "Wrong PIN"); a change of it also shakes the pad.
|
||
* captureKeyboard (default true): digits/Backspace typed anywhere go to the pad,
|
||
* except while the user is typing in a field.
|
||
* backspaceLabel / submitLabel / progressLabel(n, length) override the UiLabelsProvider texts.
|
||
*/
|
||
export function PinPad({
|
||
length = 4, minLength, maxLength, onComplete, onChange, disabled = false, error, title,
|
||
captureKeyboard = true, backspaceLabel, submitLabel, progressLabel, className,
|
||
}) {
|
||
const labels = useLabels()
|
||
const backspaceText = backspaceLabel ?? labels.backspace
|
||
const submitText = submitLabel ?? labels.submit
|
||
const progress = progressLabel ?? labels.pinProgress
|
||
const variable = minLength != null && maxLength != null
|
||
const max = variable ? maxLength : length
|
||
const [value, setValue] = useState('')
|
||
const [busy, setBusy] = useState(false)
|
||
const [shake, setShake] = useState(0)
|
||
const latest = useRef({ value, busy, disabled })
|
||
useLayoutEffect(() => { latest.current = { value, busy, disabled } })
|
||
|
||
const fail = useCallback(() => {
|
||
setShake(n => n + 1)
|
||
setValue('')
|
||
onChange?.('')
|
||
}, [onChange])
|
||
|
||
const submit = useCallback(async pin => {
|
||
let ok = true
|
||
try {
|
||
const result = onComplete?.(pin)
|
||
if (result && typeof result.then === 'function') {
|
||
setBusy(true)
|
||
ok = (await result) !== false
|
||
} else {
|
||
ok = result !== false
|
||
}
|
||
} catch {
|
||
ok = false
|
||
} finally {
|
||
setBusy(false)
|
||
}
|
||
if (ok) { setValue(''); onChange?.('') } else fail()
|
||
}, [onComplete, onChange, fail])
|
||
|
||
const press = useCallback(key => {
|
||
const { value: current, busy: isBusy, disabled: isDisabled } = latest.current
|
||
const action = pinAction(key)
|
||
if (!action || isBusy || isDisabled) return
|
||
if (action.type === 'submit') {
|
||
if (variable && canSubmit(current, minLength)) submit(current)
|
||
return
|
||
}
|
||
const next = nextPin(current, action, max)
|
||
if (next === current) return
|
||
setValue(next)
|
||
onChange?.(next)
|
||
if (!variable && next.length === max) submit(next)
|
||
}, [variable, minLength, max, onChange, submit])
|
||
|
||
useEffect(() => {
|
||
if (!captureKeyboard) return
|
||
const onKey = e => {
|
||
if (e.ctrlKey || e.metaKey || e.altKey) return
|
||
const t = e.target
|
||
if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.isContentEditable)) return
|
||
const action = pinAction(e.key)
|
||
if (!action || (action.type === 'submit' && !variable)) return // Enter only means "OK" in variable mode
|
||
e.preventDefault()
|
||
press(e.key)
|
||
}
|
||
document.addEventListener('keydown', onKey)
|
||
return () => document.removeEventListener('keydown', onKey)
|
||
}, [captureKeyboard, press, variable])
|
||
|
||
return (
|
||
<div className={cx('x-pinpad', disabled && 'x-pinpad--disabled', className)}>
|
||
{title && <div className="x-pinpad__title">{title}</div>}
|
||
{/* Re-keyed on every failed attempt and on every new error text, which replays the shake */}
|
||
<div key={`${shake}:${error || ''}`} className={cx('x-pinpad__dots', (shake > 0 || error) && 'x-pinpad__dots--shake')} aria-hidden="true">
|
||
{Array.from({ length: variable ? Math.max(minLength, value.length) : max }, (_, i) => (
|
||
<span key={i} className={cx('x-pinpad__dot', i < value.length && 'x-pinpad__dot--filled', error && 'x-pinpad__dot--error')} />
|
||
))}
|
||
</div>
|
||
<div className="x-visually-hidden" aria-live="polite">{progress(value.length, variable ? minLength : max)}</div>
|
||
<div className="x-pinpad__status">
|
||
{busy ? <Spinner size="sm" /> : error ? <span className="x-pinpad__error" role="alert">{error}</span> : null}
|
||
</div>
|
||
<div className="x-pinpad__keys">
|
||
{(variable ? KEYS_VARIABLE : KEYS).map(k => k === null
|
||
? <span key="blank" aria-hidden="true" />
|
||
: (
|
||
<button
|
||
key={k}
|
||
type="button"
|
||
className={cx('x-pinpad__key', k === 'Backspace' && 'x-pinpad__key--backspace', k === 'Enter' && 'x-pinpad__key--submit')}
|
||
onClick={() => press(k)}
|
||
disabled={disabled || busy || (k === 'Enter' && !canSubmit(value, minLength))}
|
||
aria-label={k === 'Backspace' ? backspaceText : k === 'Enter' ? submitText : undefined}
|
||
>
|
||
{k === 'Enter'
|
||
? (
|
||
<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="2.25" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
|
||
<path d="M5 12.5l4.5 4.5L19 7.5" />
|
||
</svg>
|
||
)
|
||
: k === 'Backspace'
|
||
? (
|
||
<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
|
||
<path d="M21 5H9l-6 7 6 7h12a1 1 0 0 0 1-1V6a1 1 0 0 0-1-1z" /><path d="M17 9l-6 6M11 9l6 6" />
|
||
</svg>
|
||
)
|
||
: k}
|
||
</button>
|
||
))}
|
||
</div>
|
||
</div>
|
||
)
|
||
}
|