import { Form } from '@inertiajs/react'; import { ShieldCheck } from 'lucide-react'; import { useEffect, useRef, useState } from 'react'; import { toast } from 'sonner'; import { Heading } from '@/components/layout'; import { TwoFactorRecoveryCodes, TwoFactorSetupModal } from '@/components/auth'; import { Button } from '@/components/ui/button'; import { useTwoFactorAuth } from '@/hooks/use-two-factor-auth'; import { disable, enable } from '@/routes/two-factor'; export type Props = { canManageTwoFactor?: boolean; requiresConfirmation?: boolean; twoFactorEnabled?: boolean; }; export default function ManageTwoFactor(props: Props) { const requiresConfirmation = props.requiresConfirmation ?? false; const twoFactorEnabled = props.twoFactorEnabled ?? false; const { qrCodeSvg, hasSetupData, manualSetupKey, clearSetupData, clearTwoFactorAuthData, fetchSetupData, recoveryCodesList, fetchRecoveryCodes, errors, } = useTwoFactorAuth(); const [showSetupModal, setShowSetupModal] = useState(false); const prevTwoFactorEnabled = useRef(twoFactorEnabled); useEffect(() => { if (prevTwoFactorEnabled.current && !twoFactorEnabled) { clearTwoFactorAuthData(); } prevTwoFactorEnabled.current = twoFactorEnabled; }, [twoFactorEnabled, clearTwoFactorAuthData]); if (!(props.canManageTwoFactor ?? false)) { return null; } return (
{twoFactorEnabled ? (

You will be prompted for a secure, random pin during login, which you can retrieve from the TOTP-supported application on your phone.

{ toast.error('Ada data yang belum sesuai, silakan periksa kembali input Anda.'); }} > {({ processing }) => ( )}
) : (

When you enable two-factor authentication, you will be prompted for a secure pin during login. This pin can be retrieved from a TOTP-supported application on your phone.

{hasSetupData ? ( ) : (
setShowSetupModal(true)} onError={() => { toast.error('Ada data yang belum sesuai, silakan periksa kembali input Anda.'); }} > {({ processing }) => ( )}
)}
)} setShowSetupModal(false)} requiresConfirmation={requiresConfirmation} twoFactorEnabled={twoFactorEnabled} qrCodeSvg={qrCodeSvg} manualSetupKey={manualSetupKey} clearSetupData={clearSetupData} fetchSetupData={fetchSetupData} errors={errors} />
); }