// Dependencies import { useState, useContext } from 'react' import { useTranslation } from 'next-i18next' // Hooks import { useAccount } from 'shared/hooks/use-account.mjs' import { useBackend } from 'shared/hooks/use-backend.mjs' import { useToast } from 'shared/hooks/use-toast.mjs' // Context import { LoadingContext } from 'shared/context/loading-context.mjs' // Components import { BackToAccountButton } from './shared.mjs' import { Popout } from 'shared/components/popout.mjs' import { Bullet } from 'shared/components/bullet.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="number" placeholder={t('000000')} /> ) export const MfaSettings = ({ title = false, welcome = false }) => { // Context const { loading, startLoading, stopLoading } = useContext(LoadingContext) // Hooks const { account, setAccount, token } = useAccount() const backend = useBackend(token) const { t } = useTranslation(ns) const toast = useToast() // State const [enable, setEnable] = useState(false) const [disable, setDisable] = useState(false) const [code, setCode] = useState('') const [password, setPassword] = useState('') // Helper method to enable MFA const enableMfa = async () => { startLoading() const result = await backend.enableMfa() if (result.success) setEnable(result.data.mfa) stopLoading() } // Helper method to disable MFA const disableMfa = async () => { startLoading() const result = await backend.disableMfa({ mfa: false, password, token: code, }) if (result) { if (result.success) { setAccount(result.data.account) toast.warning({t('mfaDisabled')}) } else toast.for.backendError() setDisable(false) setEnable(false) setCode('') setPassword('') } stopLoading() } // Helper method to confirm MFA const confirmMfa = async () => { startLoading() const result = await backend.confirmMfa({ mfa: true, secret: enable.secret, token: code, }) if (result.success) { setAccount(result.data.account) toast.success({t('mfaEnabled')}) } else toast.for.backendError() setEnable(false) setCode('') stopLoading() } // Figure out what title to use let titleText = account.mfaEnabled ? t('mfaEnabled') : t('mfaDisabled') if (enable) titleText = t('mfaSetup') return (
{t('mfaTipMsg')}