accounts-frontend/packages/app/pages/profile/MultiFactorAuthPage.tsx

86 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
import React from 'react';
import { RouteComponentProps } from 'react-router-dom';
import PropTypes from 'prop-types';
2017-09-09 19:52:19 +05:30
import { connect } from 'react-redux';
import MultiFactorAuth, {
MfaStep,
} from 'app/components/profile/multiFactorAuth';
import { FormModel } from 'app/components/ui/form';
import { User } from 'app/components/user';
import { RootState } from 'app/reducers';
2019-12-07 16:58:52 +05:30
interface Props
extends RouteComponentProps<{
step?: '1' | '2' | '3';
}> {
user: User;
}
2017-07-22 21:27:38 +05:30
2019-12-07 16:58:52 +05:30
class MultiFactorAuthPage extends React.Component<Props> {
static contextTypes = {
onSubmit: PropTypes.func.isRequired,
goToProfile: PropTypes.func.isRequired,
};
componentWillMount() {
const { step } = this.props.match.params;
const { user } = this.props;
if (step) {
if (!/^[1-3]$/.test(step)) {
// wrong param value
this.props.history.push('/404');
return;
}
if (user.isOtpEnabled) {
this.props.history.push('/mfa');
}
2017-07-22 21:27:38 +05:30
}
}
render() {
const { user } = this.props;
return (
<MultiFactorAuth
isMfaEnabled={user.isOtpEnabled}
onSubmit={this.onSubmit}
step={this.getStep()}
onChangeStep={this.onChangeStep}
onComplete={this.onComplete}
/>
);
}
getStep(): MfaStep {
2019-12-07 16:58:52 +05:30
const step = Number(this.props.match.params.step) - 1;
if (step !== 0 && step !== 1 && step !== 2) {
return 1;
2017-07-22 21:27:38 +05:30
}
return step;
}
onChangeStep = (step: MfaStep) => {
this.props.history.push(`/profile/mfa/step${step + 1}`);
};
2017-07-22 21:27:38 +05:30
2019-12-07 16:58:52 +05:30
onSubmit = (form: FormModel, sendData: () => Promise<void>) => {
return this.context.onSubmit({
form,
sendData,
});
};
2017-07-22 21:27:38 +05:30
onComplete = () => {
this.context.goToProfile();
};
2017-07-22 21:27:38 +05:30
}
2019-12-07 16:58:52 +05:30
export default connect(({ user }: RootState) => ({ user }))(
MultiFactorAuthPage,
);