2017-08-20 21:15:21 +05:30
|
|
|
// @flow
|
2019-06-30 19:02:50 +05:30
|
|
|
import type { RouterHistory, Match } from 'react-router-dom';
|
2017-08-04 10:21:41 +05:30
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-09-09 19:52:19 +05:30
|
|
|
import { connect } from 'react-redux';
|
2017-07-22 21:27:38 +05:30
|
|
|
|
2017-09-09 19:52:19 +05:30
|
|
|
import MultiFactorAuth from 'components/profile/multiFactorAuth';
|
|
|
|
import type { MfaStep } from 'components/profile/multiFactorAuth';
|
2017-08-20 21:15:21 +05:30
|
|
|
import type { FormModel } from 'components/ui/form';
|
2017-09-09 19:52:19 +05:30
|
|
|
import type { User } from 'components/user';
|
2017-07-22 21:27:38 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
type OwnProps = {|
|
2019-11-27 14:33:32 +05:30
|
|
|
history: RouterHistory,
|
|
|
|
match: {
|
|
|
|
...Match,
|
|
|
|
params: {
|
|
|
|
step?: '1' | '2' | '3',
|
|
|
|
},
|
|
|
|
},
|
2019-06-30 19:02:50 +05:30
|
|
|
|};
|
|
|
|
|
|
|
|
type Props = {
|
2019-11-27 14:33:32 +05:30
|
|
|
...OwnProps,
|
|
|
|
user: User,
|
|
|
|
};
|
2019-01-28 00:42:58 +05:30
|
|
|
|
|
|
|
class MultiFactorAuthPage extends Component<Props> {
|
2019-11-27 14:33:32 +05:30
|
|
|
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
|
|
|
}
|
2019-11-27 14:33:32 +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 {
|
|
|
|
const step = (parseInt(this.props.match.params.step, 10) || 1) - 1;
|
|
|
|
|
|
|
|
if (step !== 0 && step !== 1 && step !== 2) {
|
|
|
|
// NOTE: flow does not understand Array.includes()
|
|
|
|
return 1;
|
2017-07-22 21:27:38 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return step;
|
|
|
|
}
|
2017-09-09 20:34:26 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onChangeStep = (step: MfaStep) => {
|
|
|
|
this.props.history.push(`/profile/mfa/step${step + 1}`);
|
|
|
|
};
|
2017-07-22 21:27:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onSubmit = (form: FormModel, sendData: () => Promise<*>) => {
|
|
|
|
return this.context.onSubmit({
|
|
|
|
form,
|
|
|
|
sendData,
|
|
|
|
});
|
|
|
|
};
|
2017-07-22 21:27:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onComplete = () => {
|
|
|
|
this.context.goToProfile();
|
|
|
|
};
|
2017-07-22 21:27:38 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export default connect<Props, OwnProps, _, _, _, _>(({ user }) => ({ user }))(
|
|
|
|
MultiFactorAuthPage,
|
|
|
|
);
|