"use client" import { useState } from "react" import { Button } from "./ui/button" import { Input } from "./ui/input" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "./ui/dialog" import { AlertCircle, CheckCircle, Copy, Shield, Check } from "lucide-react" import { getApiUrl } from "../lib/api-config" interface TwoFactorSetupProps { open: boolean onClose: () => void onSuccess: () => void } export function TwoFactorSetup({ open, onClose, onSuccess }: TwoFactorSetupProps) { const [step, setStep] = useState(1) const [qrCode, setQrCode] = useState("") const [secret, setSecret] = useState("") const [backupCodes, setBackupCodes] = useState([]) const [verificationCode, setVerificationCode] = useState("") const [error, setError] = useState("") const [loading, setLoading] = useState(false) const [copiedSecret, setCopiedSecret] = useState(false) const [copiedCodes, setCopiedCodes] = useState(false) const handleSetupStart = async () => { setError("") setLoading(true) try { const token = localStorage.getItem("proxmenux-auth-token") const response = await fetch(getApiUrl("/api/auth/totp/setup"), { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, }) const data = await response.json() if (!response.ok) { throw new Error(data.message || "Failed to setup 2FA") } setQrCode(data.qr_code) setSecret(data.secret) setBackupCodes(data.backup_codes) setStep(2) } catch (err) { setError(err instanceof Error ? err.message : "Failed to setup 2FA") } finally { setLoading(false) } } const handleVerify = async () => { if (!verificationCode || verificationCode.length !== 6) { setError("Please enter a 6-digit code") return } setError("") setLoading(true) try { const token = localStorage.getItem("proxmenux-auth-token") const response = await fetch(getApiUrl("/api/auth/totp/enable"), { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, body: JSON.stringify({ token: verificationCode }), }) const data = await response.json() if (!response.ok) { throw new Error(data.message || "Invalid verification code") } setStep(3) } catch (err) { setError(err instanceof Error ? err.message : "Verification failed") } finally { setLoading(false) } } const copyToClipboard = (text: string, type: "secret" | "codes") => { navigator.clipboard.writeText(text) if (type === "secret") { setCopiedSecret(true) setTimeout(() => setCopiedSecret(false), 2000) } else { setCopiedCodes(true) setTimeout(() => setCopiedCodes(false), 2000) } } const handleClose = () => { setStep(1) setQrCode("") setSecret("") setBackupCodes([]) setVerificationCode("") setError("") onClose() } const handleFinish = () => { handleClose() onSuccess() } return ( Setup Two-Factor Authentication Add an extra layer of security to your account {error && (

{error}

)} {step === 1 && (

Two-factor authentication (2FA) adds an extra layer of security by requiring a code from your authentication app in addition to your password.

You will need:

  • An authentication app (Google Authenticator, Authy, etc.)
  • Scan a QR code or enter a key manually
  • Store backup codes securely
)} {step === 2 && (

1. Scan the QR code

Open your authentication app and scan this QR code

{qrCode && (
QR Code
)}

Or enter the key manually:

2. Enter the verification code

Enter the 6-digit code that appears in your app

setVerificationCode(e.target.value.replace(/\D/g, "").slice(0, 6))} className="text-center text-lg tracking-widest font-mono text-base" maxLength={6} disabled={loading} />
)} {step === 3 && (

2FA Enabled Successfully

Your account is now protected with two-factor authentication

Important: Save your backup codes

These codes will allow you to access your account if you lose access to your authentication app. Store them in a safe place.

Backup Codes
{backupCodes.map((code, index) => (
{code}
))}
)}
) }