2019-12-23 01:03:44 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
2020-03-19 04:32:07 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2019-12-23 01:03:44 +00:00
|
|
|
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';
|
2020-07-03 05:23:25 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import Field from '@/components/elements/Field';
|
2020-07-04 22:05:44 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2019-12-23 01:03:44 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
code: string;
|
|
|
|
}
|
|
|
|
|
2020-07-03 05:23:25 +00:00
|
|
|
export default ({ onDismissed, ...props }: RequiredModalProps) => {
|
2019-12-23 01:03:44 +00:00
|
|
|
const [ token, setToken ] = useState('');
|
|
|
|
const [ loading, setLoading ] = useState(true);
|
2020-07-03 05:23:25 +00:00
|
|
|
const [ recoveryTokens, setRecoveryTokens ] = useState<string[]>([]);
|
2019-12-23 04:23:43 +00:00
|
|
|
|
|
|
|
const updateUserData = useStoreActions((actions: Actions<ApplicationStore>) => actions.user.updateUserData);
|
2020-10-17 22:46:46 +00:00
|
|
|
const { clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-12-23 01:03:44 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2019-12-23 04:23:43 +00:00
|
|
|
getTwoFactorTokenUrl()
|
|
|
|
.then(setToken)
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-10-23 04:33:06 +00:00
|
|
|
clearAndAddHttpError({ error, key: 'account:two-factor' });
|
2019-12-23 04:23:43 +00:00
|
|
|
});
|
|
|
|
}, []);
|
2019-12-23 01:03:44 +00:00
|
|
|
|
2020-03-19 04:32:07 +00:00
|
|
|
const submit = ({ code }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
2019-12-23 01:03:44 +00:00
|
|
|
enableAccountTwoFactor(code)
|
2020-07-03 05:23:25 +00:00
|
|
|
.then(tokens => {
|
|
|
|
setRecoveryTokens(tokens);
|
2019-12-23 01:03:44 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
|
2020-10-23 04:33:06 +00:00
|
|
|
clearAndAddHttpError({ error, key: 'account:two-factor' });
|
2020-07-03 05:23:25 +00:00
|
|
|
})
|
|
|
|
.then(() => setSubmitting(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
const dismiss = () => {
|
|
|
|
if (recoveryTokens.length > 0) {
|
|
|
|
updateUserData({ useTotp: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
onDismissed();
|
2019-12-23 01:03:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{ code: '' }}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
code: string()
|
|
|
|
.required('You must provide an authentication code to continue.')
|
|
|
|
.matches(/^(\d){6}$/, 'Authenticator code must be 6 digits.'),
|
|
|
|
})}
|
|
|
|
>
|
2020-07-04 22:05:44 +00:00
|
|
|
{({ isSubmitting }) => (
|
2019-12-23 01:03:44 +00:00
|
|
|
<Modal
|
2019-12-23 04:23:43 +00:00
|
|
|
{...props}
|
2020-10-05 08:53:11 +00:00
|
|
|
top={false}
|
2020-07-03 05:23:25 +00:00
|
|
|
onDismissed={dismiss}
|
2019-12-23 01:03:44 +00:00
|
|
|
dismissable={!isSubmitting}
|
|
|
|
showSpinnerOverlay={loading || isSubmitting}
|
2020-07-03 05:23:25 +00:00
|
|
|
closeOnEscape={!recoveryTokens}
|
|
|
|
closeOnBackground={!recoveryTokens}
|
2019-12-23 01:03:44 +00:00
|
|
|
>
|
2020-07-03 05:23:25 +00:00
|
|
|
{recoveryTokens.length > 0 ?
|
|
|
|
<>
|
2020-07-04 22:05:44 +00:00
|
|
|
<h2 css={tw`text-2xl mb-4`}>Two-factor authentication enabled</h2>
|
|
|
|
<p css={tw`text-neutral-300`}>
|
2020-07-03 05:23:25 +00:00
|
|
|
Two-factor authentication has been enabled on your account. Should you loose access to
|
2020-07-04 22:05:44 +00:00
|
|
|
this device you'll need to use on of the codes displayed below in order to access your
|
2020-07-03 05:23:25 +00:00
|
|
|
account.
|
|
|
|
</p>
|
2020-07-04 22:05:44 +00:00
|
|
|
<p css={tw`text-neutral-300 mt-4`}>
|
2020-07-03 05:23:25 +00:00
|
|
|
<strong>These codes will not be displayed again.</strong> Please take note of them now
|
|
|
|
by storing them in a secure repository such as a password manager.
|
|
|
|
</p>
|
2020-07-04 22:05:44 +00:00
|
|
|
<pre css={tw`text-sm mt-4 rounded font-mono bg-neutral-900 p-4`}>
|
|
|
|
{recoveryTokens.map(token => <code key={token} css={tw`block mb-1`}>{token}</code>)}
|
2020-07-03 05:23:25 +00:00
|
|
|
</pre>
|
2020-07-04 22:05:44 +00:00
|
|
|
<div css={tw`text-right`}>
|
2020-07-12 04:59:34 +00:00
|
|
|
<Button css={tw`mt-6`} onClick={dismiss}>
|
2020-07-03 05:23:25 +00:00
|
|
|
Close
|
2020-07-04 22:05:44 +00:00
|
|
|
</Button>
|
2019-12-23 01:03:44 +00:00
|
|
|
</div>
|
2020-07-03 05:23:25 +00:00
|
|
|
</>
|
|
|
|
:
|
2020-07-04 22:05:44 +00:00
|
|
|
<Form css={tw`mb-0`}>
|
|
|
|
<FlashMessageRender css={tw`mb-6`} byKey={'account:two-factor'}/>
|
|
|
|
<div css={tw`flex flex-wrap`}>
|
|
|
|
<div css={tw`w-full md:flex-1`}>
|
|
|
|
<div css={tw`w-32 h-32 md:w-64 md:h-64 bg-neutral-600 p-2 rounded mx-auto`}>
|
2020-07-03 05:23:25 +00:00
|
|
|
{!token || !token.length ?
|
|
|
|
<img
|
|
|
|
src={'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='}
|
2020-07-04 22:05:44 +00:00
|
|
|
css={tw`w-64 h-64 rounded`}
|
2020-07-03 05:23:25 +00:00
|
|
|
/>
|
|
|
|
:
|
|
|
|
<img
|
|
|
|
src={`https://api.qrserver.com/v1/create-qr-code/?size=500x500&data=${token}`}
|
|
|
|
onLoad={() => setLoading(false)}
|
2020-07-04 22:05:44 +00:00
|
|
|
css={tw`w-full h-full shadow-none rounded-none`}
|
2020-07-03 05:23:25 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
</div>
|
2019-12-23 01:03:44 +00:00
|
|
|
</div>
|
2020-07-04 22:05:44 +00:00
|
|
|
<div css={tw`w-full mt-6 md:mt-0 md:flex-1 md:flex md:flex-col`}>
|
|
|
|
<div css={tw`flex-1`}>
|
2020-07-03 05:23:25 +00:00
|
|
|
<Field
|
|
|
|
id={'code'}
|
|
|
|
name={'code'}
|
|
|
|
type={'text'}
|
|
|
|
title={'Code From Authenticator'}
|
|
|
|
description={'Enter the code from your authenticator device after scanning the QR image.'}
|
|
|
|
autoFocus={!loading}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-07-04 22:05:44 +00:00
|
|
|
<div css={tw`mt-6 md:mt-0 text-right`}>
|
|
|
|
<Button>
|
2020-07-03 05:23:25 +00:00
|
|
|
Setup
|
2020-07-04 22:05:44 +00:00
|
|
|
</Button>
|
2020-07-03 05:23:25 +00:00
|
|
|
</div>
|
2019-12-23 01:03:44 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-07-03 05:23:25 +00:00
|
|
|
</Form>
|
|
|
|
}
|
2019-12-23 01:03:44 +00:00
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|