Files
gatehouse-ui/src/components/security/TotpRemoveDialog.tsx
T

161 lines
4.7 KiB
TypeScript
Raw Normal View History

2026-01-11 09:46:53 +00:00
import { useState } from "react";
import { AlertTriangle, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
2026-01-12 06:28:36 +00:00
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
2026-01-11 09:46:53 +00:00
import {
AlertDialog,
AlertDialogContent,
AlertDialogDescription,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { useToast } from "@/hooks/use-toast";
2026-01-12 06:28:36 +00:00
import { api, ApiError } from "@/lib/api";
2026-01-11 09:46:53 +00:00
interface TotpRemoveDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
onSuccess: () => void;
isRequired?: boolean;
2026-03-04 18:43:12 +05:45
hasPassword?: boolean;
2026-01-11 09:46:53 +00:00
}
export function TotpRemoveDialog({
open,
onOpenChange,
onSuccess,
isRequired = false,
2026-03-04 18:43:12 +05:45
hasPassword = true,
2026-01-11 09:46:53 +00:00
}: TotpRemoveDialogProps) {
const [isLoading, setIsLoading] = useState(false);
2026-01-12 06:28:36 +00:00
const [password, setPassword] = useState("");
const [error, setError] = useState<string | null>(null);
2026-01-11 09:46:53 +00:00
const { toast } = useToast();
2026-01-12 06:28:36 +00:00
const resetDialog = () => {
setPassword("");
setError(null);
setIsLoading(false);
};
const handleClose = (isOpen: boolean) => {
if (!isOpen) {
resetDialog();
}
onOpenChange(isOpen);
};
2026-01-11 09:46:53 +00:00
const handleRemove = async () => {
2026-03-04 18:43:12 +05:45
if (hasPassword && !password) {
2026-01-12 06:28:36 +00:00
setError("Password is required to disable TOTP");
return;
}
2026-01-11 09:46:53 +00:00
setIsLoading(true);
2026-01-12 06:28:36 +00:00
setError(null);
2026-01-11 09:46:53 +00:00
try {
2026-03-04 18:43:12 +05:45
await api.totp.disable(hasPassword ? password : null);
2026-01-11 09:46:53 +00:00
toast({
title: "Two-factor authentication disabled",
description: "TOTP has been removed from your account.",
});
onSuccess();
2026-01-12 06:28:36 +00:00
handleClose(false);
2026-01-11 09:46:53 +00:00
} catch (err) {
console.error("Failed to remove TOTP:", err);
2026-01-12 06:28:36 +00:00
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.");
}
2026-01-11 09:46:53 +00:00
} finally {
setIsLoading(false);
}
};
2026-01-12 06:28:36 +00:00
const handleKeyDown = (e: React.KeyboardEvent) => {
2026-03-04 18:43:12 +05:45
if (e.key === "Enter" && (!hasPassword || password)) {
2026-01-12 06:28:36 +00:00
handleRemove();
}
};
2026-01-11 09:46:53 +00:00
return (
2026-01-12 06:28:36 +00:00
<AlertDialog open={open} onOpenChange={handleClose}>
2026-01-11 09:46:53 +00:00
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle className="flex items-center gap-2">
<AlertTriangle className="w-5 h-5 text-destructive" />
Remove Two-Factor Authentication?
</AlertDialogTitle>
<AlertDialogDescription className="space-y-3">
<p>
This will disable TOTP-based two-factor authentication for your account.
Your backup codes will also be invalidated.
</p>
{isRequired && (
<div className="p-3 bg-destructive/10 border border-destructive/30 rounded-lg text-destructive text-sm">
<strong>Warning:</strong> Your organization requires two-factor authentication.
You may lose access to certain features if you disable it.
</div>
)}
</AlertDialogDescription>
</AlertDialogHeader>
2026-01-12 06:28:36 +00:00
<div className="space-y-4 mt-4">
2026-03-04 18:43:12 +05:45
{hasPassword && (
<div className="space-y-2">
<Label htmlFor="password-confirm">Enter your password to confirm</Label>
<Input
id="password-confirm"
type="password"
placeholder="Your current password"
value={password}
onChange={(e) => {
setPassword(e.target.value);
setError(null);
}}
onKeyDown={handleKeyDown}
disabled={isLoading}
autoFocus
/>
{error && (
<p className="text-sm text-destructive">{error}</p>
)}
</div>
)}
{!hasPassword && error && (
<p className="text-sm text-destructive">{error}</p>
)}
2026-01-12 06:28:36 +00:00
<div className="flex justify-end gap-2">
<Button
variant="outline"
onClick={() => handleClose(false)}
disabled={isLoading}
>
Cancel
</Button>
<Button
variant="destructive"
onClick={handleRemove}
2026-03-04 18:43:12 +05:45
disabled={isLoading || (hasPassword && !password)}
2026-01-12 06:28:36 +00:00
>
{isLoading && <Loader2 className="w-4 h-4 mr-2 animate-spin" />}
Remove TOTP
</Button>
</div>
2026-01-11 09:46:53 +00:00
</div>
</AlertDialogContent>
</AlertDialog>
);
}