accounts-frontend/src/services/authFlow/LoginState.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

import logger from 'services/logger';
2017-08-23 00:09:08 +05:30
import { getLogin } from 'components/auth/reducer';
2016-03-02 02:06:14 +05:30
import AbstractState from './AbstractState';
import PasswordState from './PasswordState';
import RegisterState from './RegisterState';
2019-12-07 16:58:52 +05:30
import { AuthContext } from './AuthFlow';
2016-03-02 02:06:14 +05:30
export default class LoginState extends AbstractState {
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());
}
}
resolve(
context: AuthContext,
payload: {
2019-12-07 16:58:52 +05:30
login: string;
},
) {
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
}