import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { QRCodeSVG } from 'qrcode.react'; // Use SVG for sharp rendering import {apiPath} from "../../auth/utils"; import { Box, Button, Typography, TextField, Card, CardContent, Alert, CircularProgress } from '@mui/material'; const Profile = () => { const [qrUrl, setQrUrl] = useState(''); const [code, setCode] = useState(''); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const [error, setError] = useState(''); useEffect(() => { // Fetch current user or check Redux store for twoFactorEnabled // Hide "Enable" button if already true }, []); const start2FASetup = async () => { setLoading(true); setMessage(''); setError(''); try { const response = await axios.post(`${apiPath}/2fa/setup`, {}, { headers: { Authorization: `Bearer ${localStorage.getItem('accessToken')}` } // Adjust to your auth method }); setQrUrl(response.data.otpauthUrl); } catch (err) { setError('Failed to start 2FA setup. Please try again.'); } finally { setLoading(false); } }; const verifyCode = async () => { setLoading(true); setMessage(''); setError(''); try { await axios.post(`${apiPath}/2fa/verify-setup`, { code }, { headers: { Authorization: `Bearer ${localStorage.getItem('accessToken')}` } }); setMessage('2FA enabled successfully! You will now be prompted for a code on login.'); setQrUrl(''); setCode(''); } catch (err) { setError('Invalid or expired code. Try again.'); } finally { setLoading(false); } }; return ( Profile & Security Two-Factor Authentication (2FA) {!qrUrl ? ( <> Add an extra layer of security to your account with 2FA using Microsoft Authenticator. ) : ( Scan this QR code with Microsoft Authenticator: After scanning, enter the 6-digit code from the app: setCode(e.target.value.replace(/\D/g, '').slice(0, 6))} inputProps={{ maxLength: 6 }} sx={{ width: 200 }} /> )} {message && {message}} {error && {error}} {/* Add other profile fields here later */} ); }; export default Profile;