import { type ReactNode, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { InputOTP, InputOTPGroup, InputOTPSlot } from '@/components/ui/input-otp'; import { authErrorKey } from '@/lib/auth-errors'; /** * A second-factor code: six digits from the authenticator app, or one of the * backup codes shown at enrollment. Shared by the sign-in second step * (MFAVerify) and the step-up prompt, so both read and behave the same. * * `onSubmit` throws on a wrong code; the form shows the translated reason and * clears the field for another try. */ export function TwoFactorCodeForm({ onSubmit, submitLabel, allowBackupCode = true, children, }: { onSubmit: (code: string, kind: 'totp' | 'backup') => Promise; submitLabel?: string; allowBackupCode?: boolean; children?: ReactNode; }) { const { t } = useTranslation(); const [kind, setKind] = useState<'totp' | 'backup'>('totp'); const [code, setCode] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const submit = async (value = code) => { const trimmed = value.trim(); if (kind === 'totp' && trimmed.length !== 6) { setError(t('auth.mfa.codeLength')); return; } if (kind === 'backup' && !trimmed) return; setError(''); setLoading(true); try { await onSubmit(trimmed, kind); } catch (err) { setError(t(authErrorKey(err, 'auth.errors.invalidCode'))); setCode(''); } finally { setLoading(false); } }; const switchKind = () => { setKind(kind === 'totp' ? 'backup' : 'totp'); setCode(''); setError(''); }; return (
{ e.preventDefault(); submit(); }} > {error && (
{error}
)} {kind === 'totp' ? (
submit(value)} disabled={loading} aria-label={t('auth.mfa.codeLabel')} >
) : ( setCode(e.target.value)} placeholder={t('auth.mfa.backupPlaceholder')} aria-label={t('auth.mfa.backupLabel')} autoComplete="one-time-code" disabled={loading} autoFocus /> )}
{children}
{allowBackupCode && ( )}
); }