import React, { useEffect, useState } from 'react'; import Modal, { RequiredModalProps } from '@/components/elements/Modal'; import { Form, Formik, FormikHelpers } from 'formik'; import { object, string } from 'yup'; import getTwoFactorTokenUrl from '@/api/account/getTwoFactorTokenUrl'; import enableAccountTwoFactor from '@/api/account/enableAccountTwoFactor'; import { Actions, useStoreActions } from 'easy-peasy'; import { ApplicationStore } from '@/state'; import FlashMessageRender from '@/components/FlashMessageRender'; import Field from '@/components/elements/Field'; import tw from 'twin.macro'; import Button from '@/components/elements/Button'; interface Values { code: string; } export default ({ onDismissed, ...props }: RequiredModalProps) => { const [ token, setToken ] = useState(''); const [ loading, setLoading ] = useState(true); const [ recoveryTokens, setRecoveryTokens ] = useState([]); const updateUserData = useStoreActions((actions: Actions) => actions.user.updateUserData); const { clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); useEffect(() => { getTwoFactorTokenUrl() .then(setToken) .catch(error => { console.error(error); clearAndAddHttpError({ error, key: 'account:two-factor' }); }); }, []); const submit = ({ code }: Values, { setSubmitting }: FormikHelpers) => { enableAccountTwoFactor(code) .then(tokens => { setRecoveryTokens(tokens); }) .catch(error => { console.error(error); clearAndAddHttpError({ error, key: 'account:two-factor' }); }) .then(() => setSubmitting(false)); }; const dismiss = () => { if (recoveryTokens.length > 0) { updateUserData({ useTotp: true }); } onDismissed(); }; return ( {({ isSubmitting }) => ( {recoveryTokens.length > 0 ? <>

Two-factor authentication enabled

Two-factor authentication has been enabled on your account. Should you loose access to this device you'll need to use one of the codes displayed below in order to access your account.

These codes will not be displayed again. Please take note of them now by storing them in a secure repository such as a password manager.

                                {recoveryTokens.map(token => {token})}
                            
:
{!token || !token.length ? : setLoading(false)} css={tw`w-full h-full shadow-none rounded-none`} /> }
}
)}
); };