import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { regenerateRecoveryCodes } from '@/routes/two-factor'; import { Form } from '@inertiajs/react'; import { Eye, EyeOff, LockKeyhole, RefreshCw } from 'lucide-react'; import { useCallback, useEffect, useRef, useState } from 'react'; import AlertError from './alert-error'; interface TwoFactorRecoveryCodesProps { recoveryCodesList: string[]; fetchRecoveryCodes: () => Promise; errors: string[]; } export default function TwoFactorRecoveryCodes({ recoveryCodesList, fetchRecoveryCodes, errors, }: TwoFactorRecoveryCodesProps) { const [codesAreVisible, setCodesAreVisible] = useState(false); const codesSectionRef = useRef(null); const canRegenerateCodes = recoveryCodesList.length > 0 && codesAreVisible; const toggleCodesVisibility = useCallback(async () => { if (!codesAreVisible && !recoveryCodesList.length) { await fetchRecoveryCodes(); } setCodesAreVisible(!codesAreVisible); if (!codesAreVisible) { setTimeout(() => { codesSectionRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest', }); }); } }, [codesAreVisible, recoveryCodesList.length, fetchRecoveryCodes]); useEffect(() => { if (!recoveryCodesList.length) { fetchRecoveryCodes(); } }, [recoveryCodesList.length, fetchRecoveryCodes]); const RecoveryCodeIconComponent = codesAreVisible ? EyeOff : Eye; return ( Recovery codes let you regain access if you lose your 2FA device. Store them in a secure password manager.
{canRegenerateCodes && (
{({ processing }) => ( )}
)}
{errors?.length ? ( ) : ( <>
{recoveryCodesList.length ? ( recoveryCodesList.map((code, index) => (
{code}
)) ) : (
{Array.from( { length: 8 }, (_, index) => ( )}

Each recovery code can be used once to access your account and will be removed after use. If you need more, click{' '} Regenerate Codes {' '} above.

)}
); }