// Dependencies import { useState } 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 { useLoadingStatus } from 'shared/hooks/use-loading-status.mjs' // Components import { BackToAccountButton } from './shared.mjs' import { Popout } from 'shared/components/popout/index.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 }) => { // Hooks const { account, setAccount } = useAccount() const backend = useBackend() const { t } = useTranslation(ns) const { setLoadingStatus, LoadingStatus } = useLoadingStatus() // 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 () => { 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) 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 (
{title ?

{titleText}

: null} {enable ? ( <>
{t('mfaAdd')} {t('confirmWithMfa')} setCode(evt.target.value)} className="input w-64 m-auto text-4xl input-bordered input-lg flex flex-row text-center mb-8 tracking-widest" type="number" placeholder={t('000000')} /> ) : null} {disable ? (
{t('confirmWithPassword')}
setPassword(evt.target.value)} className="input w-full input-bordered flex flex-row" type="text" placeholder={t('passwordPlaceholder')} />
{t('confirmWithMfa')}
) : null}
{account.mfaEnabled ? ( disable ? null : ( ) ) : enable ? null : (
{t('mfaTipTitle')}

{t('mfaTipMsg')}

)}
{!welcome && }
) }