2017-08-23 00:09:08 +05:30
|
|
|
// @flow
|
2016-12-10 21:28:47 +05:30
|
|
|
import logger from 'services/logger';
|
2017-08-23 00:09:08 +05:30
|
|
|
import { getCredentials } from 'components/auth/reducer';
|
2018-03-01 03:28:02 +05:30
|
|
|
import { getActiveAccount, getAvailableAccounts } from 'components/accounts/reducer';
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2016-03-02 02:06:14 +05:30
|
|
|
import AbstractState from './AbstractState';
|
2018-02-18 01:29:35 +05:30
|
|
|
import ChooseAccountState from './ChooseAccountState';
|
2016-03-02 02:06:14 +05:30
|
|
|
import CompleteState from './CompleteState';
|
|
|
|
import ForgotPasswordState from './ForgotPasswordState';
|
2016-03-02 02:22:59 +05:30
|
|
|
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');
|
2016-05-15 02:23:58 +05:30
|
|
|
} 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
|
|
|
|
}
|
|
|
|
) {
|
2018-02-18 01:29:35 +05:30
|
|
|
const { login, returnUrl } = 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,
|
2016-06-04 19:16:39 +05:30
|
|
|
rememberMe,
|
2016-11-13 02:01:44 +05:30
|
|
|
login
|
2016-03-02 02:06:14 +05:30
|
|
|
})
|
2017-08-23 02:30:10 +05:30
|
|
|
.then(() => {
|
2018-02-18 01:29:35 +05:30
|
|
|
const { isTotpRequired } = getCredentials(context.getState());
|
2017-08-23 02:30:10 +05:30
|
|
|
|
|
|
|
if (isTotpRequired) {
|
|
|
|
return context.setState(new MfaState());
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:29:35 +05:30
|
|
|
if (returnUrl) {
|
|
|
|
context.navigate(returnUrl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-23 02:30:10 +05:30
|
|
|
return context.setState(new CompleteState());
|
|
|
|
})
|
|
|
|
.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) {
|
2018-03-01 03:28:02 +05:30
|
|
|
const state = context.getState();
|
|
|
|
const { isRelogin } = getCredentials(state);
|
2018-02-18 01:29:35 +05:30
|
|
|
|
|
|
|
if (isRelogin) {
|
2018-03-01 03:28:02 +05:30
|
|
|
const availableAccounts = getAvailableAccounts(state);
|
|
|
|
const accountToRemove = getActiveAccount(state);
|
|
|
|
|
|
|
|
if (availableAccounts.length === 1 || !accountToRemove) {
|
|
|
|
context.run('logout');
|
|
|
|
context.run('setLogin', null);
|
|
|
|
context.setState(new LoginState());
|
|
|
|
} else {
|
|
|
|
const accountToReplace = availableAccounts.find(({id}) => id !== accountToRemove.id);
|
|
|
|
|
|
|
|
context.run('activateAccount', accountToReplace);
|
|
|
|
context.run('removeAccount', accountToRemove);
|
|
|
|
context.setState(new ChooseAccountState());
|
|
|
|
}
|
2018-02-18 01:29:35 +05:30
|
|
|
} else {
|
|
|
|
context.run('setLogin', null);
|
|
|
|
context.setState(new LoginState());
|
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|
|
|
|
}
|