2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2019-12-10 13:17:32 +05:30
|
|
|
import { RouteComponentProps, Redirect } from 'react-router-dom';
|
2017-09-09 19:52:19 +05:30
|
|
|
import { connect } from 'react-redux';
|
2019-12-08 00:32:00 +05:30
|
|
|
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-12 12:56:23 +05:30
|
|
|
import Context from 'app/components/profile/Context';
|
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> {
|
2019-12-12 12:56:23 +05:30
|
|
|
static contextType = Context;
|
|
|
|
/* TODO: use declare */ context: React.ContextType<typeof Context>;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-10 13:17:32 +05:30
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
user,
|
|
|
|
match: {
|
|
|
|
params: { step },
|
|
|
|
},
|
|
|
|
} = this.props;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
if (step) {
|
|
|
|
if (!/^[1-3]$/.test(step)) {
|
|
|
|
// wrong param value
|
2019-12-10 13:17:32 +05:30
|
|
|
return <Redirect to="/404" />;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (user.isOtpEnabled) {
|
2019-12-10 13:17:32 +05:30
|
|
|
return <Redirect to="/mfa" />;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2017-07-22 21:27:38 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
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;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
if (step !== 0 && step !== 1 && step !== 2) {
|
2019-12-10 13:17:32 +05:30
|
|
|
return 0;
|
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
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
onChangeStep = (step: number) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
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>) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
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-12-07 16:58:52 +05:30
|
|
|
export default connect(({ user }: RootState) => ({ user }))(
|
2019-11-27 14:33:32 +05:30
|
|
|
MultiFactorAuthPage,
|
|
|
|
);
|