41 lines
1.4 KiB
TypeScript
Raw Normal View History

import logger from 'app/services/logger';
2017-08-22 21:39:08 +03:00
import { getCredentials } from 'app/components/auth/reducer';
2017-08-22 21:39:08 +03:00
import AbstractState from './AbstractState';
import CompleteState from './CompleteState';
import PasswordState from './PasswordState';
2019-12-07 13:28:52 +02:00
import { AuthContext } from './AuthFlow';
2017-08-22 21:39:08 +03:00
export default class MfaState extends AbstractState {
2020-05-24 02:08:24 +03:00
enter(context: AuthContext): Promise<void> | void {
const { login, password, isTotpRequired } = getCredentials(context.getState());
2020-05-24 02:08:24 +03:00
if (login && password && isTotpRequired) {
context.navigate('/mfa');
} else {
context.setState(new CompleteState());
}
2017-08-22 21:39:08 +03:00
}
2020-05-24 02:08:24 +03:00
resolve(context: AuthContext, { totp }: { totp: string }): Promise<void> | void {
const { login, password, rememberMe, isRelogin } = getCredentials(context.getState());
2020-05-24 02:08:24 +03:00
return context
.run('login', {
// @ts-ignore there will be no invalid value
login,
2020-05-24 02:08:24 +03:00
password,
totp,
2020-05-24 02:08:24 +03:00
rememberMe,
})
.then(() => !isRelogin && context.run('setAccountSwitcher', false))
2020-05-24 02:08:24 +03:00
.then(() => context.setState(new CompleteState()))
.catch((err = {}) => err.errors || logger.warn('Error logging in', err));
}
2020-05-24 02:08:24 +03:00
goBack(context: AuthContext) {
context.setState(new PasswordState());
}
2017-08-22 21:39:08 +03:00
}