import { useState, useEffect } from 'react'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { MFAChallenge } from './MFAChallenge'; import { Shield, AlertCircle, Loader2 } from 'lucide-react'; import { getEnrolledFactors } from '@/lib/authService'; import { useAuth } from '@/hooks/useAuth'; import { handleError } from '@/lib/errorHandler'; interface AutoMFAVerificationModalProps { open: boolean; onSuccess: () => void; onCancel: () => void; } export function AutoMFAVerificationModal({ open, onSuccess, onCancel }: AutoMFAVerificationModalProps) { const { session } = useAuth(); const [factorId, setFactorId] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); // Fetch enrolled factor automatically when modal opens useEffect(() => { if (!open || !session) return; const fetchFactor = async () => { setLoading(true); setError(null); try { const factors = await getEnrolledFactors(); if (factors.length === 0) { setError('No MFA method enrolled. Please set up MFA in settings.'); return; } // Use the first verified TOTP factor const totpFactor = factors.find(f => f.factor_type === 'totp'); if (totpFactor) { setFactorId(totpFactor.id); } else { setError('No valid MFA method found. Please check your security settings.'); } } catch (err) { setError('Failed to load MFA settings. Please try again.'); handleError(err, { action: 'Fetch MFA Factors for Auto-Verification', metadata: { context: 'AutoMFAVerificationModal' } }); } finally { setLoading(false); } }; fetchFactor(); }, [open, session]); return ( { if (!isOpen) { onCancel(); } }} > e.preventDefault()} onEscapeKeyDown={(e) => e.preventDefault()} >
Verification Required
Your session requires Multi-Factor Authentication to access this area.
{loading && (

Loading verification...

)} {error && (

{error}

)} {!loading && !error && factorId && ( )}
); }