2017-12-31 00:34:31 +05:30
|
|
|
// @flow
|
|
|
|
import { getActiveAccount } from 'components/accounts/reducer';
|
2016-03-02 02:06:14 +05:30
|
|
|
import AbstractState from './AbstractState';
|
|
|
|
import LoginState from './LoginState';
|
|
|
|
import PermissionsState from './PermissionsState';
|
2016-11-13 20:17:56 +05:30
|
|
|
import ChooseAccountState from './ChooseAccountState';
|
2016-03-02 02:06:14 +05:30
|
|
|
import ActivationState from './ActivationState';
|
2016-08-03 00:29:29 +05:30
|
|
|
import AcceptRulesState from './AcceptRulesState';
|
2016-03-15 12:06:13 +05:30
|
|
|
import FinishState from './FinishState';
|
2017-12-31 00:34:31 +05:30
|
|
|
import type { AuthContext } from './AuthFlow';
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2016-11-19 20:11:15 +05:30
|
|
|
const PROMPT_ACCOUNT_CHOOSE = 'select_account';
|
|
|
|
const PROMPT_PERMISSIONS = 'consent';
|
|
|
|
|
2016-03-02 02:06:14 +05:30
|
|
|
export default class CompleteState extends AbstractState {
|
2017-12-31 00:34:31 +05:30
|
|
|
isPermissionsAccepted: bool | void;
|
|
|
|
|
|
|
|
constructor(options: {
|
|
|
|
accept?: bool,
|
|
|
|
} = {}) {
|
|
|
|
super();
|
2016-03-15 12:06:13 +05:30
|
|
|
|
2016-03-21 11:46:37 +05:30
|
|
|
this.isPermissionsAccepted = options.accept;
|
2016-03-15 12:06:13 +05:30
|
|
|
}
|
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
enter(context: AuthContext) {
|
2017-01-29 17:12:51 +05:30
|
|
|
const {auth = {}, user} = context.getState();
|
2016-03-02 02:06:14 +05:30
|
|
|
|
|
|
|
if (user.isGuest) {
|
|
|
|
context.setState(new LoginState());
|
|
|
|
} else if (!user.isActive) {
|
|
|
|
context.setState(new ActivationState());
|
2016-08-03 00:29:29 +05:30
|
|
|
} else if (user.shouldAcceptRules) {
|
|
|
|
context.setState(new AcceptRulesState());
|
2016-03-16 10:33:23 +05:30
|
|
|
} else if (auth.oauth && auth.oauth.clientId) {
|
2017-01-29 17:12:51 +05:30
|
|
|
return this.processOAuth(context);
|
|
|
|
} else {
|
|
|
|
context.navigate('/');
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 20:11:15 +05:30
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
processOAuth(context: AuthContext) {
|
2017-01-29 17:12:51 +05:30
|
|
|
const {auth = {}, accounts} = context.getState();
|
2016-11-19 20:11:15 +05:30
|
|
|
|
2017-01-29 17:12:51 +05:30
|
|
|
let isSwitcherEnabled = auth.isSwitcherEnabled;
|
|
|
|
const loginHint = auth.oauth.loginHint;
|
2016-11-19 20:11:15 +05:30
|
|
|
|
2017-01-29 17:12:51 +05:30
|
|
|
if (loginHint) {
|
|
|
|
const account = accounts.available.filter((account) =>
|
|
|
|
account.id === loginHint * 1
|
|
|
|
|| account.email === loginHint
|
|
|
|
|| account.username === loginHint
|
|
|
|
)[0];
|
2017-12-31 00:34:31 +05:30
|
|
|
const activeAccount = getActiveAccount(context.getState());
|
2017-01-29 17:12:51 +05:30
|
|
|
|
|
|
|
if (account) {
|
|
|
|
// disable switching, because we are know the account, user must be authorized with
|
|
|
|
context.run('setAccountSwitcher', false);
|
|
|
|
isSwitcherEnabled = false;
|
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
if (!activeAccount || account.id !== activeAccount.id) {
|
2017-01-29 17:12:51 +05:30
|
|
|
// lets switch user to an account, that is needed for auth
|
|
|
|
return context.run('authenticate', account)
|
|
|
|
.then(() => context.setState(new CompleteState()));
|
2016-11-19 20:11:15 +05:30
|
|
|
}
|
|
|
|
}
|
2017-01-29 17:12:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (isSwitcherEnabled
|
|
|
|
&& (accounts.available.length > 1
|
|
|
|
|| auth.oauth.prompt.includes(PROMPT_ACCOUNT_CHOOSE)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
context.setState(new ChooseAccountState());
|
|
|
|
} else if (auth.oauth.code) {
|
|
|
|
context.setState(new FinishState());
|
|
|
|
} else {
|
|
|
|
const data = {};
|
|
|
|
if (typeof this.isPermissionsAccepted !== 'undefined') {
|
|
|
|
data.accept = this.isPermissionsAccepted;
|
|
|
|
} else if (auth.oauth.acceptRequired || auth.oauth.prompt.includes(PROMPT_PERMISSIONS)) {
|
|
|
|
context.setState(new PermissionsState());
|
|
|
|
return;
|
|
|
|
}
|
2016-11-19 20:11:15 +05:30
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
// TODO: it seems that oAuthComplete may be a separate state
|
|
|
|
return context.run('oAuthComplete', data).then((resp: {
|
|
|
|
redirectUri: string,
|
|
|
|
}) => {
|
2017-01-29 17:12:51 +05:30
|
|
|
// TODO: пусть в стейт попадает флаг или тип авторизации
|
|
|
|
// вместо волшебства над редирект урлой
|
|
|
|
if (resp.redirectUri.indexOf('static_page') === 0) {
|
|
|
|
context.setState(new FinishState());
|
|
|
|
} else {
|
2017-02-01 11:41:36 +05:30
|
|
|
return context.run('redirect', resp.redirectUri);
|
2017-01-29 17:12:51 +05:30
|
|
|
}
|
|
|
|
}, (resp) => {
|
|
|
|
if (resp.unauthorized) {
|
|
|
|
context.setState(new LoginState());
|
|
|
|
} else if (resp.acceptRequired) {
|
2016-04-15 01:24:35 +05:30
|
|
|
context.setState(new PermissionsState());
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|
2017-01-29 17:12:51 +05:30
|
|
|
});
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|