import { useState } from 'react'; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { AlertCircle, CheckCircle2, Loader2, QrCode } from 'lucide-react'; import authApi from '@/api/auth.api'; interface TwoFactorSetupProps { onComplete?: () => void; } export default function TwoFactorSetup({ onComplete }: TwoFactorSetupProps) { const [step, setStep] = useState<'initial' | 'setup' | 'verify' | 'success'>('initial'); const [qrCode, setQrCode] = useState(''); const [otp, setOtp] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const startSetup = async () => { setIsLoading(true); setError(null); try { const data = await authApi.setup2FA(); setQrCode(data.qrCode); setStep('setup'); } catch (err: any) { setError(err.response?.data?.message || "Failed to start 2FA setup."); } finally { setIsLoading(false); } }; const verifySetup = async () => { if (!otp) return; setIsLoading(true); setError(null); try { await authApi.verify2FA(otp); setStep('success'); } catch (err: any) { setError(err.response?.data?.message || "Invalid OTP. Please try again."); } finally { setIsLoading(false); } }; const handleComplete = () => { if (onComplete) { onComplete(); } else { setStep('initial'); } }; return ( Two-Factor Authentication Secure your account with Google Authenticator. {step === 'initial' && (

Two-factor authentication adds an extra layer of security to your account. Once enabled, you'll need to enter a code from your Google Authenticator app when logging in.

{error &&

{error}

}
)} {step === 'setup' && (
2FA QR Code

Open Google Authenticator and scan the QR code above.

setOtp(e.target.value)} maxLength={6} />
{error && (
{error}
)}
)} {step === 'success' && (

2FA Enabled Successfully

Your account is now secured with two-factor authentication.

)}
); }