accounts-frontend/packages/app/components/profile/multiFactorAuth/MfaDisable.tsx

60 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
import React from 'react';
import PropTypes from 'prop-types';
import logger from 'app/services/logger';
import { disable as disableMFA } from 'app/services/api/mfa';
import { FormModel } from 'app/components/ui/form';
2017-09-09 19:52:19 +05:30
import MfaDisableForm from './disableForm/MfaDisableForm';
import MfaStatus from './status/MfaStatus';
2019-12-07 16:58:52 +05:30
export default class MfaDisable extends React.Component<
{
2019-12-07 16:58:52 +05:30
onSubmit: (form: FormModel, sendData: () => Promise<void>) => Promise<void>;
onComplete: () => void;
},
{
2019-12-07 16:58:52 +05:30
showForm: boolean;
}
> {
static contextTypes = {
userId: PropTypes.number.isRequired,
};
2019-12-07 16:58:52 +05:30
state = {
showForm: false,
};
render() {
const { showForm } = this.state;
return showForm ? (
<MfaDisableForm onSubmit={this.onSubmit} />
) : (
<MfaStatus onProceed={this.onProceed} />
);
}
onProceed = () => this.setState({ showForm: true });
onSubmit = (form: FormModel) => {
return this.props
.onSubmit(form, () => {
2019-12-07 16:58:52 +05:30
const { totp } = form.serialize() as { totp: string };
2019-12-07 16:58:52 +05:30
return disableMFA(this.context.userId, totp);
})
.then(() => this.props.onComplete())
.catch(resp => {
const { errors } = resp || {};
if (errors) {
return Promise.reject(errors);
}
logger.error('MFA: Unexpected disable form result', {
resp,
});
});
};
2017-09-09 19:52:19 +05:30
}