2021-05-16 19:35:49 +00:00
|
|
|
import React, { useContext } from 'react';
|
2020-03-19 04:32:07 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2019-12-23 04:41:25 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { object, string } from 'yup';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import disableAccountTwoFactor from '@/api/account/disableAccountTwoFactor';
|
2020-07-04 19:39:55 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2021-05-16 19:35:49 +00:00
|
|
|
import asModal from '@/hoc/asModal';
|
|
|
|
import ModalContext from '@/context/ModalContext';
|
2019-12-23 04:41:25 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:35:49 +00:00
|
|
|
const DisableTwoFactorModal = () => {
|
|
|
|
const { dismiss, setPropOverrides } = useContext(ModalContext);
|
2020-10-17 22:42:52 +00:00
|
|
|
const { clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-12-23 04:41:25 +00:00
|
|
|
const updateUserData = useStoreActions((actions: Actions<ApplicationStore>) => actions.user.updateUserData);
|
|
|
|
|
2020-03-19 04:32:07 +00:00
|
|
|
const submit = ({ password }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
2021-05-16 19:35:49 +00:00
|
|
|
setPropOverrides({ showSpinnerOverlay: true, dismissable: false });
|
2019-12-23 04:41:25 +00:00
|
|
|
disableAccountTwoFactor(password)
|
|
|
|
.then(() => {
|
|
|
|
updateUserData({ useTotp: false });
|
2021-05-16 19:35:49 +00:00
|
|
|
dismiss();
|
2019-12-23 04:41:25 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
|
2020-10-23 04:33:06 +00:00
|
|
|
clearAndAddHttpError({ error, key: 'account:two-factor' });
|
2019-12-23 04:41:25 +00:00
|
|
|
setSubmitting(false);
|
2021-05-16 19:35:49 +00:00
|
|
|
setPropOverrides(null);
|
2019-12-23 04:41:25 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
password: '',
|
|
|
|
}}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
password: string().required('You must provider your current password in order to continue.'),
|
|
|
|
})}
|
|
|
|
>
|
2021-05-16 19:35:49 +00:00
|
|
|
{({ isValid }) => (
|
|
|
|
<Form className={'mb-0'}>
|
|
|
|
<FlashMessageRender css={tw`mb-6`} byKey={'account:two-factor'}/>
|
|
|
|
<Field
|
|
|
|
id={'password'}
|
|
|
|
name={'password'}
|
|
|
|
type={'password'}
|
|
|
|
label={'Current Password'}
|
|
|
|
description={'In order to disable two-factor authentication you will need to provide your account password.'}
|
|
|
|
autoFocus
|
|
|
|
/>
|
|
|
|
<div css={tw`mt-6 text-right`}>
|
|
|
|
<Button color={'red'} disabled={!isValid}>
|
|
|
|
Disable Two-Factor
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
2019-12-23 04:41:25 +00:00
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
2021-05-16 19:35:49 +00:00
|
|
|
|
|
|
|
export default asModal()(DisableTwoFactorModal);
|