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

78 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-12-07 13:28:52 +02:00
import React from 'react';
2019-12-10 09:47:32 +02:00
import { RouteComponentProps, Redirect } from 'react-router-dom';
import { connect } from 'app/functions';
2020-05-24 02:08:24 +03:00
import MultiFactorAuth, { MfaStep } from 'app/components/profile/multiFactorAuth';
import { FormModel } from 'app/components/ui/form';
import { User } from 'app/components/user';
2019-12-12 09:26:23 +02:00
import Context from 'app/components/profile/Context';
2019-12-07 13:28:52 +02:00
interface Props
2020-05-24 02:08:24 +03:00
extends RouteComponentProps<{
step?: '1' | '2' | '3';
}> {
user: User;
2019-12-07 13:28:52 +02:00
}
2017-07-22 18:57:38 +03:00
2019-12-07 13:28:52 +02:00
class MultiFactorAuthPage extends React.Component<Props> {
2020-05-24 02:08:24 +03:00
static contextType = Context;
/* TODO: use declare */ context: React.ContextType<typeof Context>;
2020-05-24 02:08:24 +03:00
render() {
const {
user,
match: {
params: { step },
},
} = this.props;
2020-05-24 02:08:24 +03:00
if (step) {
if (!/^[1-3]$/.test(step)) {
// wrong param value
return <Redirect to="/404" />;
}
2020-05-24 02:08:24 +03:00
if (user.isOtpEnabled) {
return <Redirect to="/mfa" />;
}
}
return (
<MultiFactorAuth
isMfaEnabled={user.isOtpEnabled}
onSubmit={this.onSubmit}
step={this.getStep()}
onChangeStep={this.onChangeStep}
onComplete={this.onComplete}
/>
);
2017-07-22 18:57:38 +03:00
}
2020-05-24 02:08:24 +03:00
getStep(): MfaStep {
const step = Number(this.props.match.params.step) - 1;
2020-05-24 02:08:24 +03:00
if (step !== 0 && step !== 1 && step !== 2) {
return 0;
}
2020-05-24 02:08:24 +03:00
return step;
2017-07-22 18:57:38 +03:00
}
2020-05-24 02:08:24 +03:00
onChangeStep = (step: number) => {
this.props.history.push(`/profile/mfa/step${step + 1}`);
};
2017-07-22 18:57:38 +03:00
2020-05-24 02:08:24 +03:00
onSubmit = (form: FormModel, sendData: () => Promise<void>) => {
return this.context.onSubmit({
form,
sendData,
});
};
2017-07-22 18:57:38 +03:00
2020-05-24 02:08:24 +03:00
onComplete = () => {
this.context.goToProfile();
};
2017-07-22 18:57:38 +03:00
}
export default connect(({ user }) => ({ user }))(MultiFactorAuthPage);