import { useState } from 'react'; import { supabase } from '@/lib/supabase'; import { useAuth } from '@/contexts/AuthContext'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Shield, Copy, Check, AlertCircle } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; export function TwoFactorSetup() { const { user } = useAuth(); const { toast } = useToast(); const [isEnabled, setIsEnabled] = useState(false); const [secret, setSecret] = useState(''); const [backupCodes, setBackupCodes] = useState([]); const [verificationCode, setVerificationCode] = useState(''); const [isLoading, setIsLoading] = useState(false); const [showSetup, setShowSetup] = useState(false); const [copiedCodes, setCopiedCodes] = useState(false); const handleSetup = async () => { setIsLoading(true); try { const { data, error } = await supabase.functions.invoke('two-factor-auth', { body: { action: 'setup', userId: user?.id } }); if (error) throw error; setSecret(data.secret); setBackupCodes(data.backupCodes); setShowSetup(true); toast({ title: '2FA Setup Started', description: 'Scan the QR code with your authenticator app', }); } catch (error) { toast({ title: 'Setup Failed', description: error.message, variant: 'destructive', }); } finally { setIsLoading(false); } }; const handleEnable = async () => { if (!verificationCode) { toast({ title: 'Error', description: 'Please enter verification code', variant: 'destructive', }); return; } setIsLoading(true); try { const { data, error } = await supabase.functions.invoke('two-factor-auth', { body: { action: 'enable', userId: user?.id, code: verificationCode } }); if (error) throw error; setIsEnabled(true); setShowSetup(false); toast({ title: '2FA Enabled', description: 'Your account is now protected with two-factor authentication', }); } catch (error) { toast({ title: 'Verification Failed', description: error.message, variant: 'destructive', }); } finally { setIsLoading(false); } }; const handleDisable = async () => { setIsLoading(true); try { const { data, error } = await supabase.functions.invoke('two-factor-auth', { body: { action: 'disable', userId: user?.id } }); if (error) throw error; setIsEnabled(false); setSecret(''); setBackupCodes([]); toast({ title: '2FA Disabled', description: 'Two-factor authentication has been disabled', }); } catch (error) { toast({ title: 'Error', description: error.message, variant: 'destructive', }); } finally { setIsLoading(false); } }; const copyBackupCodes = () => { navigator.clipboard.writeText(backupCodes.join('\n')); setCopiedCodes(true); setTimeout(() => setCopiedCodes(false), 2000); }; return ( Two-Factor Authentication Add an extra layer of security to your account {!isEnabled && !showSetup && (
Two-factor authentication adds an extra layer of security by requiring a code from your authenticator app
)} {showSetup && (

1. Scan QR Code

Use your authenticator app to scan this QR code or enter the secret manually

{secret}

2. Save Backup Codes

Store these codes in a safe place. You can use them to access your account if you lose your device.

{backupCodes.map((code, i) => (
{code}
))}

3. Verify Setup

Enter the 6-digit code from your authenticator app

setVerificationCode(e.target.value)} maxLength={6} />
)} {isEnabled && (
Two-factor authentication is enabled for your account
)}
); }