'use client'; import { AlertBanner, Box, Button, Code, Column, Dialog, Icon, Image, Modal, Row, Tag, TagGroup, Text, } from '@umami/react-zen'; import { useQueryClient } from '@tanstack/react-query'; import { ReactNode, useEffect, useState } from 'react'; import { OtpInput } from '@/components/common/OtpInput'; import { useMessages, useUpdateQuery } from '@/components/hooks'; import { TwoFactorSuccessModal } from './TwoFactorSuccessModal'; import styles from './TwoFactorSetupModal.module.css'; import { LucideCopy } from 'lucide-react'; function Step({ tag, title, details, children, }: { tag: string; title: string; details: string; children: ReactNode; }) { return ( {tag} {title} {details} {children} ); } interface TwoFactorSetupModalProps { required: boolean; onClose?: () => void; } export function TwoFactorSetupModal({ required, onClose }: TwoFactorSetupModalProps) { const { t, labels, messages } = useMessages(); const [qrCodeDataUrl, setQrCodeDataUrl] = useState(null); const [manualKey, setManualKey] = useState(null); const [otpValue, setOtpValue] = useState(''); const [copied, setCopied] = useState(false); const [error, setError] = useState(null); const [backupCodes, setBackupCodes] = useState(null); const queryClient = useQueryClient(); const { mutate: initiate } = useUpdateQuery('/2fa/setup/initiate'); const { mutateAsync: confirm, isPending: isConfirming } = useUpdateQuery('/2fa/setup/confirm'); const { mutate: cancel } = useUpdateQuery('/2fa/setup/cancel'); useEffect(() => { initiate( {}, { onSuccess: (data: any) => { setQrCodeDataUrl(data.qrCodeDataUrl); setManualKey(data.manualKey); }, onError: () => setError(t(messages.error)), }, ); }, []); const handleConfirm = async (value?: string) => { const token = value ?? otpValue; if (token.length !== 6) return; setError(null); try { const data: any = await confirm({ token }); setBackupCodes(data.backupCodes); } catch (err: any) { setError(err.message || t(messages.error)); } }; const handleCancel = async () => { cancel({}); onClose?.(); }; const handleCopy = () => { if (manualKey) { navigator.clipboard.writeText(manualKey); setCopied(true); setTimeout(() => setCopied(false), 2000); } }; if (backupCodes) { return ( { queryClient.invalidateQueries({ queryKey: ['2fa-status'] }); onClose?.(); }} /> ); } const preventDismiss = required ? () => {} : undefined; return ( {({ close }) => ( {required && t(messages.twoFactorSetupRequiredDescription) + ' '} {t(messages.twoFactorSetupDescription)} {/* Step 1 - Scan QR Code */} {qrCodeDataUrl && ( )} {t(labels.twoFactorCantScan)} {t(labels.twoFactorManualEntry)} {manualKey} {copied ? t(labels.twoFactorCodeCopied) : t(labels.twoFactorCopyCode)} {/* Step 2 - Enter Verification Code */} {t(labels.twoFactorEnterCode)} { setOtpValue(val); if (error) setError(null); }} onComplete={handleConfirm} disabled={isConfirming} /> {error && } {!required && ( {t(labels.cancel)} )} handleConfirm()} isDisabled={otpValue.length !== 6 || isConfirming || !!error} > {t(labels.confirm)} )} ); }
{manualKey}