import { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { supabase } from '@/integrations/supabase/client'; import { useToast } from '@/hooks/use-toast'; import { Header } from '@/components/layout/Header'; import { MFAChallenge } from '@/components/auth/MFAChallenge'; import { Shield } from 'lucide-react'; import { getStepUpRequired, getIntendedPath, clearStepUpFlags } from '@/lib/sessionFlags'; import { getEnrolledFactors } from '@/lib/authService'; export default function MFAStepUp() { const navigate = useNavigate(); const { toast } = useToast(); const [factorId, setFactorId] = useState(null); useEffect(() => { const checkStepUpRequired = async () => { // Check if this page was accessed via proper flow if (!getStepUpRequired()) { console.log('[MFAStepUp] No step-up flag found, redirecting to auth'); navigate('/auth'); return; } // Get enrolled MFA factors const factors = await getEnrolledFactors(); if (factors.length === 0) { console.log('[MFAStepUp] No verified TOTP factor found'); toast({ variant: 'destructive', title: 'MFA not enrolled', description: 'Please enroll in two-factor authentication first.', }); clearStepUpFlags(); navigate('/settings?tab=security'); return; } setFactorId(factors[0].id); }; checkStepUpRequired(); }, [navigate, toast]); const handleSuccess = async () => { console.log('[MFAStepUp] MFA verification successful'); toast({ title: 'Verification successful', description: 'You now have full access to all features.', }); // Redirect to home or intended destination const intendedPath = getIntendedPath(); clearStepUpFlags(); navigate(intendedPath); }; const handleCancel = async () => { console.log('[MFAStepUp] MFA verification cancelled'); // Clear flags and redirect to sign-in (less harsh than forcing sign-out) clearStepUpFlags(); toast({ title: 'Verification cancelled', description: 'Please sign in again to continue.', }); navigate('/auth'); }; return (

Additional Verification Required

Your role requires two-factor authentication. Please verify your identity to continue.

{factorId ? ( ) : (
)}
); }