95 lines
3.0 KiB
TypeScript
Raw Normal View History

import logger from 'app/services/logger';
import { getCredentials } from 'app/components/auth/reducer';
2020-05-24 02:08:24 +03:00
import { getActiveAccount, getAvailableAccounts } from 'app/components/accounts/reducer';
2016-03-01 22:36:14 +02:00
import AbstractState from './AbstractState';
import ChooseAccountState from './ChooseAccountState';
2016-03-01 22:36:14 +02:00
import CompleteState from './CompleteState';
import ForgotPasswordState from './ForgotPasswordState';
import LoginState from './LoginState';
2017-08-22 21:39:08 +03:00
import MfaState from './MfaState';
2019-12-07 13:28:52 +02:00
import { AuthContext } from './AuthFlow';
2016-03-01 22:36:14 +02:00
export default class PasswordState extends AbstractState {
2020-05-24 02:08:24 +03:00
enter(context: AuthContext): Promise<void> | void {
const { login, isTotpRequired } = getCredentials(context.getState());
2016-03-01 22:36:14 +02:00
2020-05-24 02:08:24 +03:00
if (isTotpRequired) {
context.setState(new MfaState());
} else if (login) {
context.navigate('/password');
} else {
context.setState(new CompleteState());
}
2016-03-01 22:36:14 +02:00
}
2020-05-24 02:08:24 +03:00
resolve(
context: AuthContext,
{
password,
rememberMe,
}: {
password: string;
rememberMe: boolean;
},
): Promise<void> | void {
const { login, returnUrl, isRelogin } = getCredentials(context.getState());
2016-03-01 22:36:14 +02:00
2020-05-24 02:08:24 +03:00
return context
.run('login', {
password,
rememberMe,
login,
})
.then(() => {
const { isTotpRequired } = getCredentials(context.getState());
2020-05-24 02:08:24 +03:00
if (isTotpRequired) {
return context.setState(new MfaState());
}
if (!isRelogin) {
context.run('setAccountSwitcher', false);
}
2020-05-24 02:08:24 +03:00
if (returnUrl) {
context.navigate(returnUrl);
2020-05-24 02:08:24 +03:00
return;
}
2016-03-01 22:36:14 +02:00
2020-05-24 02:08:24 +03:00
return context.setState(new CompleteState());
})
.catch((err = {}) => err.errors || logger.warn('Error logging in', err));
}
2016-03-01 22:36:14 +02:00
2020-05-24 02:08:24 +03:00
reject(context: AuthContext): void {
context.setState(new ForgotPasswordState());
}
2020-05-24 02:08:24 +03:00
goBack(context: AuthContext): void {
const state = context.getState();
const { isRelogin } = getCredentials(state);
2020-05-24 02:08:24 +03:00
if (isRelogin) {
const availableAccounts = getAvailableAccounts(state);
const accountToRemove = getActiveAccount(state);
2020-05-24 02:08:24 +03:00
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);
2020-05-24 02:08:24 +03:00
context.run('activateAccount', accountToReplace);
context.run('removeAccount', accountToRemove);
context.setState(new ChooseAccountState());
}
} else {
context.run('setLogin', null);
context.setState(new LoginState());
}
2016-03-01 22:36:14 +02:00
}
}