import { IconShieldCheck as ShieldCheck, IconShieldOff as ShieldOff, IconDeviceMobile as Smartphone, } from '@tabler/icons-react'; import { QRCodeSVG } from 'qrcode.react'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useSearchParams } from 'react-router'; import { useAuth } from '@/components/auth/AuthProvider'; import { TwoFactorCodeForm } from '@/components/auth/TwoFactorCodeForm'; import { PageHeader } from '@/components/PageHeader'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Switch } from '@/components/ui/switch'; import { authErrorKey } from '@/lib/auth-errors'; import { ContentPanel } from '../../components/ContentPanel'; /** The base32 secret inside an otpauth:// URI, for typing in by hand. */ function totpSecret(totpURI: string): string { try { return new URL(totpURI).searchParams.get('secret') ?? ''; } catch { return ''; } } type Setup = { totpURI: string; backupCodes: string[] }; /** * Two-factor settings. Enrolling: (password, if the account has one) → * scan the QR code and keep the backup codes → confirm with a code from the * app. Turning it off needs a session that passed a code (the provider asks * for one when the server says so) and, for a password account, the password. */ export default function Security() { const { t } = useTranslation(); const { mfa, hasMfaEnabled, mfaRequired } = useAuth(); const [searchParams] = useSearchParams(); const [hasPassword, setHasPassword] = useState(null); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); // 'enable' | 'disable' while the confirm dialog (password and/or warning) is open const [confirming, setConfirming] = useState<'enable' | 'disable' | null>(null); const [password, setPassword] = useState(''); const [setup, setSetup] = useState(null); const setupRequired = searchParams.get('setup') === 'required'; // biome-ignore lint/correctness/useExhaustiveDependencies: load once; mfa is rebuilt each render useEffect(() => { mfa .hasPassword() .then(setHasPassword) .catch(() => setHasPassword(true)); }, []); const closeConfirm = () => { setConfirming(null); setPassword(''); }; const startEnrollment = async (pw?: string) => { setBusy(true); setError(''); try { setSetup(await mfa.enroll(pw)); closeConfirm(); } catch (err) { setError(t(authErrorKey(err, 'security.enrollFailed'))); } finally { setBusy(false); } }; const disable = async (pw?: string) => { setBusy(true); setError(''); try { await mfa.disable(pw); closeConfirm(); } catch (err) { setError(t(authErrorKey(err, 'security.disableFailed'))); } finally { setBusy(false); } }; const onToggle = (checked: boolean) => { setError(''); if (checked) { if (hasPassword) setConfirming('enable'); else startEnrollment(); } else { setConfirming('disable'); } }; const onConfirm = () => { const pw = hasPassword ? password : undefined; if (confirming === 'enable') startEnrollment(pw); else disable(pw); }; const confirmEnrollment = async (code: string) => { await mfa.confirmEnrollment(code); setSetup(null); }; return ( <> {setupRequired && !hasMfaEnabled && (

{t('security.mfaRequired')}

{t('security.mfaRequiredDescription')}

)} {error && !confirming && !setup && (
{error}
)}
{t('security.twoFactor')} {t('security.twoFactorDescription')}
{hasPassword !== null && ( )}
{(hasMfaEnabled || mfaRequired) && ( {hasMfaEnabled && (

{t('security.mfaEnabledDescription')}

)} {mfaRequired && (

{t('security.mfaRequiredByOrg')}

)}
)}
{/* Password (and, for turning it off, the warning) before enable/disable */} !open && closeConfirm()}> {confirming === 'disable' ? t('security.disableMfa') : t('security.setupMfa')} {confirming === 'disable' ? t('security.disableMfaDescription') : t('security.passwordPrompt')} {confirming === 'disable' && mfaRequired && (
{t('security.disableMfaWarning')}
)}
{ e.preventDefault(); onConfirm(); }} > {hasPassword && ( <> setPassword(e.target.value)} required autoFocus /> )} {error && (
{error}
)}
{/* Setup: QR code, manual secret, backup codes, then the first code */} !open && setSetup(null)}> {t('security.setupMfa')} {t('security.setupMfaDescription')} {setup && (

{t('security.cantScan')}

{totpSecret(setup.totpURI)}
{setup.backupCodes.length > 0 && (

{t('security.backupCodes')}

{t('security.backupCodesDescription')}

    {setup.backupCodes.map((code) => (
  • {code}
  • ))}
)}

{t('security.enterCode')}

confirmEnrollment(code)} submitLabel={t('security.verifyAndEnable')} allowBackupCode={false} />
)}
); }