accounts-frontend/src/services/authFlow/PasswordState.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-08-23 00:09:08 +05:30
// @flow
import logger from 'services/logger';
2017-08-23 00:09:08 +05:30
import { getCredentials } from 'components/auth/reducer';
2016-03-02 02:06:14 +05:30
import AbstractState from './AbstractState';
import CompleteState from './CompleteState';
import ForgotPasswordState from './ForgotPasswordState';
import LoginState from './LoginState';
2017-08-23 00:09:08 +05:30
import MfaState from './MfaState';
import type { AuthContext } from './AuthFlow';
2016-03-02 02:06:14 +05:30
export default class PasswordState extends AbstractState {
2017-08-23 00:09:08 +05:30
enter(context: AuthContext) {
const {login} = getCredentials(context.getState());
2016-03-02 02:06:14 +05:30
2017-08-23 00:09:08 +05:30
if (login) {
2016-03-02 02:06:14 +05:30
context.navigate('/password');
} else {
context.setState(new CompleteState());
2016-03-02 02:06:14 +05:30
}
}
2017-08-23 00:09:08 +05:30
resolve(
context: AuthContext,
{
password,
rememberMe
}: {
password: string,
rememberMe: bool
}
) {
const {login} = getCredentials(context.getState());
2016-03-02 02:06:14 +05:30
2017-08-23 00:36:38 +05:30
return context.run('login', {
2016-03-02 02:06:14 +05:30
password,
rememberMe,
login
2016-03-02 02:06:14 +05:30
})
2017-08-23 00:09:08 +05:30
.then(() => {
const {isTotpRequired} = getCredentials(context.getState());
if (isTotpRequired) {
return context.setState(new MfaState());
}
return context.setState(new CompleteState());
})
2017-03-02 11:28:33 +05:30
.catch((err = {}) =>
err.errors || logger.warn('Error logging in', err)
);
2016-03-02 02:06:14 +05:30
}
2017-08-23 00:09:08 +05:30
reject(context: AuthContext) {
2016-03-02 02:06:14 +05:30
context.setState(new ForgotPasswordState());
}
2017-08-23 00:09:08 +05:30
goBack(context: AuthContext) {
context.run('setLogin', null);
2016-03-02 02:06:14 +05:30
context.setState(new LoginState());
}
}