// Context import { LoadingStatusContext } from 'shared/context/loading-status-context.mjs' // Hooks import { useState, useContext } from 'react' import { useTranslation } from 'next-i18next' import { useAccount } from 'shared/hooks/use-account.mjs' import { useBackend } from 'shared/hooks/use-backend.mjs' // Components import { BackToAccountButton } from './shared.mjs' import { Popout } from 'shared/components/popout/index.mjs' import { Bullet } from 'shared/components/bullet.mjs' import { PasswordInput } from 'shared/components/inputs.mjs' import { CopyToClipboard } from 'shared/components/copy-to-clipboard.mjs' export const ns = ['account'] const CodeInput = ({ code, setCode, t }) => ( setCode(evt.target.value)} className="input w-full text-4xl input-bordered input-lg flex flex-row text-center mb-8 tracking-widest" type="text" placeholder={t('000000')} /> ) export const MfaSettings = ({ title = false, welcome = false }) => { // Hooks const { account, setAccount } = useAccount() const backend = useBackend() const { t } = useTranslation(ns) const { setLoadingStatus } = useContext(LoadingStatusContext) // State const [enable, setEnable] = useState(false) const [disable, setDisable] = useState(false) const [code, setCode] = useState('') const [password, setPassword] = useState('') const [scratchCodes, setScratchCodes] = useState(false) // Helper method to enable MFA const enableMfa = async () => { setLoadingStatus([true, 'processingUpdate']) const result = await backend.enableMfa() if (result.success) { setEnable(result.data.mfa) setLoadingStatus([true, 'settingsSaved', true, true]) } else setLoadingStatus([true, 'backendError', true, false]) } // Helper method to disable MFA const disableMfa = async () => { setLoadingStatus([true, 'processingUpdate']) const result = await backend.disableMfa({ mfa: false, password, token: code, }) if (result) { if (result.success) { setAccount(result.data.account) setLoadingStatus([true, 'settingsSaved', true, true]) } else setLoadingStatus([true, 'backendError', true, false]) setDisable(false) setEnable(false) setCode('') setPassword('') } } // Helper method to confirm MFA const confirmMfa = async () => { setLoadingStatus([true, 'processingUpdate']) const result = await backend.confirmMfa({ mfa: true, secret: enable.secret, token: code, }) if (result.success) { setAccount(result.data.account) setScratchCodes(result.data.scratchCodes) setLoadingStatus([true, 'settingsSaved', true, true]) } else setLoadingStatus([true, 'backendError', true, false]) setEnable(false) setCode('') } // Figure out what title to use let titleText = account.mfaEnabled ? t('mfaEnabled') : t('mfaDisabled') if (enable) titleText = t('mfaSetup') return (
{enable.secret}
{t('account:mfaScratchCodesMsg1')}
{t('account:mfaScratchCodesMsg2')}
{scratchCodes.map((code) => code + '\n')}
{t('mfaTipMsg')}