'use client'; import React, { useState, useEffect, useRef } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { setupTwoFactorAuth, verifyAndEnableTwoFactorAuth, disableTwoFactorAuth, getTwoFactorAuthStatus } from '@/app/API/profile/twoFactorAuth'; import './two-factor.scss'; const TwoFactorAuthPage = () => { const router = useRouter(); const [loading, setLoading] = useState(true); const [setupLoading, setSetupLoading] = useState(false); const [verifying, setVerifying] = useState(false); const [disabling, setDisabling] = useState(false); const [twoFactorEnabled, setTwoFactorEnabled] = useState(false); const [qrCode, setQrCode] = useState(null); const [secret, setSecret] = useState(null); const [verificationToken, setVerificationToken] = useState(''); const [password, setPassword] = useState(''); const [disableToken, setDisableToken] = useState(''); const [step, setStep] = useState<'info' | 'setup' | 'verification' | 'success' | 'disable'>('info'); const [error, setError] = useState(null); const [showPassword, setShowPassword] = useState(false); const inputRef = useRef(null); useEffect(() => { const fetchStatus = async () => { try { const response = await getTwoFactorAuthStatus(); if (response.status === 'success' && response.data) { setTwoFactorEnabled(response.data.enabled); setStep(response.data.enabled ? 'info' : 'info'); } } catch (err) { setError('Failed to load 2FA status'); } finally { setLoading(false); } }; fetchStatus(); }, []); useEffect(() => { if (step === 'verification' || step === 'disable') { setTimeout(() => { inputRef.current?.focus(); }, 100); } }, [step]); const handleSetupClick = async () => { setError(null); setSetupLoading(true); try { const response = await setupTwoFactorAuth(); if (response.status === 'success' && response.data) { setQrCode(response.data.qrCodeUrl); setSecret(response.data.secret); setStep('setup'); } else { throw new Error(response.message || 'Failed to set up 2FA'); } } catch (err: any) { setError(err.message || 'Error setting up 2FA'); } finally { setSetupLoading(false); } }; const handleVerify = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setVerifying(true); try { const response = await verifyAndEnableTwoFactorAuth(verificationToken); if (response.status === 'success') { setTwoFactorEnabled(true); setStep('success'); } else { throw new Error(response.message || 'Verification failed'); } } catch (err: any) { setError(err.message || 'Error verifying code'); } finally { setVerifying(false); } }; const handleDisable = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setDisabling(true); try { const response = await disableTwoFactorAuth(password, disableToken); if (response.status === 'success') { setTwoFactorEnabled(false); setStep('info'); setPassword(''); setDisableToken(''); } else { throw new Error(response.message || 'Failed to disable 2FA'); } } catch (err: any) { setError(err.message || 'Error disabling 2FA'); } finally { setDisabling(false); } }; const handleTokenChange = (e: React.ChangeEvent) => { let value = e.target.value.replace(/\D/g, '').slice(0, 6); if (step === 'verification') { setVerificationToken(value); } else { setDisableToken(value); } }; if (loading) { return (

Two-Factor Authentication

Loading...
); } return (

Two-Factor Authentication

Add an extra layer of security to your account

{error && (
{error}
)}
{step === 'info' && ( <>
Two-factor authentication is {twoFactorEnabled ? 'enabled' : 'disabled'}

{twoFactorEnabled ? 'Your account is protected with an additional layer of security.' : 'Enable two-factor authentication for enhanced account security.'}

{twoFactorEnabled && ( Active )}
What is two-factor authentication?

Two-factor authentication adds an extra layer of security to your account. In addition to your password, you'll need a verification code generated by an authenticator app on your mobile device to log in.

Authenticator App Required

You'll need an authenticator app like Google Authenticator, Microsoft Authenticator, or Authy.

{twoFactorEnabled ? ( ) : ( )} )} {step === 'setup' && qrCode && secret && ( <>
Set up your authenticator app

Scan the QR code below with your authenticator app or enter the setup key manually.

QR Code
If you can't scan the QR code, enter this text code into your authenticator app.
)} {step === 'verification' && (
Verify your authenticator app

Enter the 6-digit verification code from your authenticator app to enable two-factor authentication.

The verification code is 6 digits long.
)} {step === 'success' && (

Two-factor authentication enabled!

Your account is now protected with an additional layer of security. Each time you sign in, you'll need to provide a verification code from your authenticator app.

Important Security Notice

Keep your authenticator app safe. If you lose access to your authenticator app, you might be unable to log in to your account.

)} {step === 'disable' && (
Warning: Reducing Account Security

Disabling two-factor authentication will make your account less secure. To confirm this action, please enter your password and current verification code.

setPassword(e.target.value)} required />
)} {step !== 'info' && step !== 'disable' && step !== 'success' && (
)}
Back to Security Settings
); }; export default TwoFactorAuthPage;