import { useState } from "react"; import { AlertTriangle, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { AlertDialog, AlertDialogContent, AlertDialogDescription, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { useToast } from "@/hooks/use-toast"; import { api, ApiError } from "@/lib/api"; interface TotpRemoveDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onSuccess: () => void; isRequired?: boolean; hasPassword?: boolean; } export function TotpRemoveDialog({ open, onOpenChange, onSuccess, isRequired = false, hasPassword = true, }: TotpRemoveDialogProps) { const [isLoading, setIsLoading] = useState(false); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const { toast } = useToast(); const resetDialog = () => { setPassword(""); setError(null); setIsLoading(false); }; const handleClose = (isOpen: boolean) => { if (!isOpen) { resetDialog(); } onOpenChange(isOpen); }; const handleRemove = async () => { if (hasPassword && !password) { setError("Password is required to disable TOTP"); return; } setIsLoading(true); setError(null); try { await api.totp.disable(hasPassword ? password : null); toast({ title: "Two-factor authentication disabled", description: "TOTP has been removed from your account.", }); onSuccess(); handleClose(false); } catch (err) { console.error("Failed to remove TOTP:", err); if (err instanceof ApiError) { if (err.type === "INVALID_CREDENTIALS" || err.code === 401) { setError("Incorrect password. Please try again."); } else { setError(err.message); } } else { setError("An error occurred. Please try again."); } } finally { setIsLoading(false); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && (!hasPassword || password)) { handleRemove(); } }; return ( Remove Two-Factor Authentication?

This will disable TOTP-based two-factor authentication for your account. Your backup codes will also be invalidated.

{isRequired && (
Warning: Your organization requires two-factor authentication. You may lose access to certain features if you disable it.
)}
{hasPassword && (
{ setPassword(e.target.value); setError(null); }} onKeyDown={handleKeyDown} disabled={isLoading} autoFocus /> {error && (

{error}

)}
)} {!hasPassword && error && (

{error}

)}
); }