2016-12-07 02:38:51 +05:30
|
|
|
import logger from 'services/logger';
|
2017-08-23 00:09:08 +05:30
|
|
|
import { getLogin } from 'components/auth/reducer';
|
2016-12-07 02:38:51 +05:30
|
|
|
|
2016-03-02 02:06:14 +05:30
|
|
|
import AbstractState from './AbstractState';
|
|
|
|
import PasswordState from './PasswordState';
|
2017-03-03 11:18:25 +05:30
|
|
|
import RegisterState from './RegisterState';
|
2019-12-07 16:58:52 +05:30
|
|
|
import { AuthContext } from './AuthFlow';
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2016-03-02 02:06:14 +05:30
|
|
|
export default class LoginState extends AbstractState {
|
2019-11-27 14:33:32 +05:30
|
|
|
enter(context: AuthContext) {
|
|
|
|
const login = getLogin(context.getState());
|
|
|
|
const { user } = context.getState();
|
|
|
|
|
|
|
|
const isUserAddsSecondAccount =
|
|
|
|
!user.isGuest && /login|password/.test(context.getRequest().path); // TODO: improve me
|
|
|
|
|
|
|
|
// TODO: it may not allow user to leave password state till he click back or enters password
|
|
|
|
if (login) {
|
|
|
|
context.setState(new PasswordState());
|
|
|
|
} else if (user.isGuest || isUserAddsSecondAccount) {
|
|
|
|
context.navigate('/login');
|
|
|
|
} else {
|
|
|
|
// can not detect needed state. Delegating decision to the next state
|
|
|
|
context.setState(new PasswordState());
|
2017-01-29 21:06:21 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
resolve(
|
|
|
|
context: AuthContext,
|
|
|
|
payload: {
|
2019-12-07 16:58:52 +05:30
|
|
|
login: string;
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
) {
|
|
|
|
context
|
|
|
|
.run('login', payload)
|
|
|
|
.then(() => context.setState(new PasswordState()))
|
|
|
|
.catch(
|
|
|
|
(err = {}) => err.errors || logger.warn('Error validating login', err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
reject(context: AuthContext) {
|
|
|
|
context.setState(new RegisterState());
|
|
|
|
}
|
|
|
|
|
|
|
|
goBack(context: AuthContext) {
|
|
|
|
context.run('goBack', {
|
|
|
|
fallbackUrl: '/',
|
|
|
|
});
|
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|