2019-12-23 04:41:25 +00:00
|
|
|
import React 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 Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
|
|
|
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';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-07-04 19:39:55 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2019-12-23 04:41:25 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ ...props }: RequiredModalProps) => {
|
|
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
const updateUserData = useStoreActions((actions: Actions<ApplicationStore>) => actions.user.updateUserData);
|
|
|
|
|
2020-03-19 04:32:07 +00:00
|
|
|
const submit = ({ password }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
2019-12-23 04:41:25 +00:00
|
|
|
clearFlashes('account:two-factor');
|
|
|
|
disableAccountTwoFactor(password)
|
|
|
|
.then(() => {
|
|
|
|
updateUserData({ useTotp: false });
|
|
|
|
props.onDismissed();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
addError({ message: httpErrorToHuman(error), key: 'account:two-factor' });
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
password: '',
|
|
|
|
}}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
password: string().required('You must provider your current password in order to continue.'),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{({ isSubmitting, isValid }) => (
|
|
|
|
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
|
|
|
|
<Form className={'mb-0'}>
|
2020-07-04 19:39:55 +00:00
|
|
|
<FlashMessageRender css={tw`mb-6`} byKey={'account:two-factor'}/>
|
2019-12-23 04:41:25 +00:00
|
|
|
<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.'}
|
2020-07-04 19:39:55 +00:00
|
|
|
autoFocus
|
2019-12-23 04:41:25 +00:00
|
|
|
/>
|
2020-07-04 19:39:55 +00:00
|
|
|
<div css={tw`mt-6 text-right`}>
|
|
|
|
<Button disabled={!isValid}>
|
2019-12-23 04:41:25 +00:00
|
|
|
Disable Two-Factor
|
2020-07-04 19:39:55 +00:00
|
|
|
</Button>
|
2019-12-23 04:41:25 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|