import { SyntheticEvent, useState } from "react"; import { ErrorCode } from "@lib/auth"; import { Dialog, DialogContent } from "@components/Dialog"; import Button from "@components/ui/Button"; import TwoFactorAuthAPI from "./TwoFactorAuthAPI"; import TwoFactorModalHeader from "./TwoFactorModalHeader"; interface DisableTwoFactorAuthModalProps { /** * Called when the user closes the modal without disabling two-factor auth */ onCancel: () => void; /** * Called when the user disables two-factor auth */ onDisable: () => void; } const DisableTwoFactorAuthModal = ({ onDisable, onCancel }: DisableTwoFactorAuthModalProps) => { const [password, setPassword] = useState(""); const [isDisabling, setIsDisabling] = useState(false); const [errorMessage, setErrorMessage] = useState(null); async function handleDisable(e: SyntheticEvent) { e.preventDefault(); if (isDisabling) { return; } setIsDisabling(true); setErrorMessage(null); try { const response = await TwoFactorAuthAPI.disable(password); if (response.status === 200) { onDisable(); return; } const body = await response.json(); if (body.error === ErrorCode.IncorrectPassword) { setErrorMessage("Password is incorrect."); } else { setErrorMessage("Something went wrong."); } } catch (e) { setErrorMessage("Something went wrong."); console.error("Error disabling two-factor authentication", e); } finally { setIsDisabling(false); } } return (
setPassword(e.currentTarget.value)} className="block w-full border-gray-300 rounded-sm shadow-sm focus:ring-neutral-900 focus:border-neutral-900 sm:text-sm" />
{errorMessage &&

{errorMessage}

}
); }; export default DisableTwoFactorAuthModal;