accounts-frontend/packages/app/services/authFlow/LoginState.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

import logger from 'app/services/logger';
import { getLogin } from 'app/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 =
2019-12-29 21:56:51 +05:30
!user.isGuest && /login|password/.test(location.pathname); // 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
}