import { getAuth } from '@/auth'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { apiConfig } from '@/config/api.config'; import { useCallApi } from '@/hooks'; import { RefreshCw, Shield, Smartphone, QrCode } from 'lucide-react'; import { useState } from 'react'; import { toast } from 'sonner'; import clsx from 'clsx'; const API_URL = apiConfig.service_dashboard; const GenerateQr = () => { const { GetData, PostData } = useCallApi(); const parsedUser = getAuth()?.user; const [qrCodeUrl, setQrCodeUrl] = useState(null); const [inputToken, setInputToken] = useState(''); const [loading, setLoading] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const getQrCode = async () => { if (!parsedUser?.id) { toast.error('User ID not found'); return; } setLoading(true); setError(null); try { const response = await GetData(`${API_URL}/user/generate_mfa/${parsedUser.id}`, {}); if (response?.status) { setQrCodeUrl(response.data.token_base64); toast.success('QR Code generated successfully'); } else { setError('QR Code not found'); toast.error(response?.message); } } catch (error) { toast.error('Something went wrong, please try again'); } finally { setLoading(false); } }; const verifyToken = async () => { if (!parsedUser?.id) { toast.error('User ID not found'); return; } setIsSubmitting(true); try { const response = await PostData(`${API_URL}/user/verify_mfa`, { id: parsedUser.id, token: inputToken }); if (response?.status) { toast.success('Token verified successfully'); setInputToken(''); } else { toast.error(response?.message); } } catch (error) { toast.error('Something went wrong, please try again'); } finally { setIsSubmitting(false); setQrCodeUrl(null); } }; return (

Multi-Factor Authentication

{!qrCodeUrl ? ( // Initial Setup State

Secure Your Account

Add an extra layer of security to your account with multi-factor authentication using your mobile device.

1

Download an authenticator app

Google Authenticator{' '} click here

) : ( // QR Code Generated State

Scan QR Code

Open your authenticator app and scan this QR code

QR Code MFA
setInputToken(e.target.value.replace(/\D/g, '').slice(0, 6))} maxLength={6} />

Enter the 6-digit code from your authenticator app

)} {error && (

{error}

)}
); }; export default GenerateQr;