2019-12-08 00:32:00 +05:30
|
|
|
import { browserHistory } from 'app/services/history';
|
|
|
|
import logger from 'app/services/logger';
|
|
|
|
import localStorage from 'app/services/localStorage';
|
|
|
|
import loader from 'app/services/loader';
|
|
|
|
import history from 'app/services/history';
|
2019-11-27 14:33:32 +05:30
|
|
|
import {
|
|
|
|
updateUser,
|
|
|
|
acceptRules as userAcceptRules,
|
2019-12-08 00:32:00 +05:30
|
|
|
} from 'app/components/user/actions';
|
|
|
|
import { authenticate, logoutAll } from 'app/components/accounts/actions';
|
|
|
|
import { getActiveAccount } from 'app/components/accounts/reducer';
|
2019-01-28 00:42:58 +05:30
|
|
|
import {
|
2019-11-27 14:33:32 +05:30
|
|
|
login as loginEndpoint,
|
|
|
|
forgotPassword as forgotPasswordEndpoint,
|
|
|
|
recoverPassword as recoverPasswordEndpoint,
|
2019-12-07 16:58:52 +05:30
|
|
|
OAuthResponse,
|
2019-12-08 00:32:00 +05:30
|
|
|
} from 'app/services/api/authentication';
|
|
|
|
import oauth, { OauthData, Client, Scope } from 'app/services/api/oauth';
|
|
|
|
import signup from 'app/services/api/signup';
|
|
|
|
import dispatchBsod from 'app/components/ui/bsod/dispatchBsod';
|
|
|
|
import { create as createPopup } from 'app/components/ui/popup/actions';
|
|
|
|
import ContactForm from 'app/components/contact/ContactForm';
|
|
|
|
import { ThunkAction, Dispatch } from 'app/reducers';
|
2019-06-30 19:02:50 +05:30
|
|
|
|
2018-03-14 02:21:37 +05:30
|
|
|
import { getCredentials } from './reducer';
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
type ValidationError =
|
|
|
|
| string
|
|
|
|
| {
|
2019-12-07 16:58:52 +05:30
|
|
|
type: string;
|
|
|
|
payload: { [key: string]: any };
|
2019-11-27 14:33:32 +05:30
|
|
|
};
|
2019-06-30 19:02:50 +05:30
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
export { updateUser } from 'app/components/user/actions';
|
2018-03-01 03:28:02 +05:30
|
|
|
export {
|
2019-11-27 14:33:32 +05:30
|
|
|
authenticate,
|
|
|
|
logoutAll as logout,
|
|
|
|
remove as removeAccount,
|
|
|
|
activate as activateAccount,
|
2019-12-08 00:32:00 +05:30
|
|
|
} from 'app/components/accounts/actions';
|
|
|
|
import { Account } from 'app/components/accounts/reducer';
|
2017-01-04 11:22:46 +05:30
|
|
|
|
2017-01-29 21:06:21 +05:30
|
|
|
/**
|
|
|
|
* Reoutes user to the previous page if it is possible
|
|
|
|
*
|
2017-12-31 00:34:31 +05:30
|
|
|
* @param {object} options
|
|
|
|
* @param {string} options.fallbackUrl - an url to route user to if goBack is not possible
|
2017-01-29 21:06:21 +05:30
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {object} - action definition
|
2017-01-29 21:06:21 +05:30
|
|
|
*/
|
2019-11-27 14:33:32 +05:30
|
|
|
export function goBack(options: { fallbackUrl?: string }) {
|
|
|
|
const { fallbackUrl } = options || {};
|
|
|
|
|
|
|
|
if (history.canGoBack()) {
|
|
|
|
browserHistory.goBack();
|
|
|
|
} else if (fallbackUrl) {
|
|
|
|
browserHistory.push(fallbackUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'noop',
|
|
|
|
};
|
2017-01-29 21:06:21 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export function redirect(url: string): () => Promise<void> {
|
2019-11-27 14:33:32 +05:30
|
|
|
loader.show();
|
2017-02-01 11:52:12 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return () =>
|
|
|
|
new Promise(() => {
|
|
|
|
// do not resolve promise to make loader visible and
|
|
|
|
// overcome app rendering
|
|
|
|
location.href = url;
|
2017-02-01 11:52:12 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:16:00 +05:30
|
|
|
const PASSWORD_REQUIRED = 'error.password_required';
|
|
|
|
const LOGIN_REQUIRED = 'error.login_required';
|
|
|
|
const ACTIVATION_REQUIRED = 'error.account_not_activated';
|
|
|
|
const TOTP_REQUIRED = 'error.totp_required';
|
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
export function login({
|
2019-11-27 14:33:32 +05:30
|
|
|
login = '',
|
|
|
|
password = '',
|
|
|
|
totp,
|
|
|
|
rememberMe = false,
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
login: string;
|
|
|
|
password?: string;
|
|
|
|
totp?: string;
|
|
|
|
rememberMe?: boolean;
|
2017-08-23 00:09:08 +05:30
|
|
|
}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return wrapInLoader(dispatch =>
|
|
|
|
loginEndpoint({ login, password, totp, rememberMe })
|
|
|
|
.then(authHandler(dispatch))
|
|
|
|
.catch(resp => {
|
|
|
|
if (resp.errors) {
|
|
|
|
if (resp.errors.password === PASSWORD_REQUIRED) {
|
|
|
|
return dispatch(setLogin(login));
|
|
|
|
} else if (resp.errors.login === ACTIVATION_REQUIRED) {
|
|
|
|
return dispatch(needActivation());
|
|
|
|
} else if (resp.errors.totp === TOTP_REQUIRED) {
|
|
|
|
return dispatch(
|
|
|
|
requestTotp({
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
rememberMe,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else if (resp.errors.login === LOGIN_REQUIRED && password) {
|
|
|
|
logger.warn('No login on password panel');
|
|
|
|
|
|
|
|
return dispatch(logoutAll());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
})
|
|
|
|
.catch(validationErrorsHandler(dispatch)),
|
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2016-08-03 00:29:29 +05:30
|
|
|
export function acceptRules() {
|
2019-11-27 14:33:32 +05:30
|
|
|
return wrapInLoader(dispatch =>
|
|
|
|
dispatch(userAcceptRules()).catch(validationErrorsHandler(dispatch)),
|
|
|
|
);
|
2016-08-03 00:29:29 +05:30
|
|
|
}
|
|
|
|
|
2016-05-15 02:23:58 +05:30
|
|
|
export function forgotPassword({
|
2019-11-27 14:33:32 +05:30
|
|
|
login = '',
|
|
|
|
captcha = '',
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
login: string;
|
|
|
|
captcha: string;
|
2016-05-15 02:23:58 +05:30
|
|
|
}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return wrapInLoader((dispatch, getState) =>
|
|
|
|
forgotPasswordEndpoint(login, captcha)
|
|
|
|
.then(({ data = {} }) =>
|
|
|
|
dispatch(
|
|
|
|
updateUser({
|
|
|
|
maskedEmail: data.emailMask || getState().user.email,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.catch(validationErrorsHandler(dispatch)),
|
|
|
|
);
|
2016-05-15 02:23:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function recoverPassword({
|
2019-11-27 14:33:32 +05:30
|
|
|
key = '',
|
|
|
|
newPassword = '',
|
|
|
|
newRePassword = '',
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
key: string;
|
|
|
|
newPassword: string;
|
|
|
|
newRePassword: string;
|
2016-05-15 02:23:58 +05:30
|
|
|
}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return wrapInLoader(dispatch =>
|
|
|
|
recoverPasswordEndpoint(key, newPassword, newRePassword)
|
|
|
|
.then(authHandler(dispatch))
|
|
|
|
.catch(validationErrorsHandler(dispatch, '/forgot-password')),
|
|
|
|
);
|
2016-03-16 11:24:42 +05:30
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
export function register({
|
2019-11-27 14:33:32 +05:30
|
|
|
email = '',
|
|
|
|
username = '',
|
|
|
|
password = '',
|
|
|
|
rePassword = '',
|
|
|
|
captcha = '',
|
|
|
|
rulesAgreement = false,
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
email: string;
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
rePassword: string;
|
|
|
|
captcha: string;
|
|
|
|
rulesAgreement: boolean;
|
2016-02-13 20:58:47 +05:30
|
|
|
}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return wrapInLoader((dispatch, getState) =>
|
|
|
|
signup
|
|
|
|
.register({
|
|
|
|
email,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
rePassword,
|
|
|
|
rulesAgreement,
|
|
|
|
lang: getState().user.lang,
|
|
|
|
captcha,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
dispatch(
|
|
|
|
updateUser({
|
|
|
|
username,
|
|
|
|
email,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch(needActivation());
|
|
|
|
|
|
|
|
browserHistory.push('/activation');
|
|
|
|
})
|
|
|
|
.catch(validationErrorsHandler(dispatch)),
|
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export function activate({
|
|
|
|
key = '',
|
|
|
|
}: {
|
|
|
|
key: string;
|
|
|
|
}): ThunkAction<Promise<Account>> {
|
2019-11-27 14:33:32 +05:30
|
|
|
return wrapInLoader(dispatch =>
|
|
|
|
signup
|
|
|
|
.activate({ key })
|
|
|
|
.then(authHandler(dispatch))
|
|
|
|
.catch(validationErrorsHandler(dispatch, '/resend-activation')),
|
|
|
|
);
|
2016-05-23 00:28:43 +05:30
|
|
|
}
|
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
export function resendActivation({
|
2019-11-27 14:33:32 +05:30
|
|
|
email = '',
|
|
|
|
captcha,
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
email: string;
|
|
|
|
captcha: string;
|
2017-08-23 00:09:08 +05:30
|
|
|
}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return wrapInLoader(dispatch =>
|
|
|
|
signup
|
|
|
|
.resendActivation({ email, captcha })
|
|
|
|
.then(resp => {
|
|
|
|
dispatch(
|
|
|
|
updateUser({
|
|
|
|
email,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
})
|
|
|
|
.catch(validationErrorsHandler(dispatch)),
|
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2017-02-24 02:37:05 +05:30
|
|
|
export function contactUs() {
|
2019-11-27 14:33:32 +05:30
|
|
|
return createPopup({ Popup: ContactForm });
|
2017-02-24 02:37:05 +05:30
|
|
|
}
|
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
export const SET_CREDENTIALS = 'auth:setCredentials';
|
|
|
|
/**
|
|
|
|
* Sets login in credentials state
|
|
|
|
*
|
|
|
|
* Resets the state, when `null` is passed
|
|
|
|
*
|
|
|
|
* @param {string|null} login
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {object}
|
2017-08-23 00:09:08 +05:30
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
export function setLogin(login: string | null) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
type: SET_CREDENTIALS,
|
|
|
|
payload: login
|
|
|
|
? {
|
|
|
|
login,
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
};
|
2017-08-23 00:09:08 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export function relogin(login: string | null): ThunkAction {
|
|
|
|
return (dispatch, getState) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const credentials = getCredentials(getState());
|
|
|
|
const returnUrl =
|
|
|
|
credentials.returnUrl || location.pathname + location.search;
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: SET_CREDENTIALS,
|
|
|
|
payload: {
|
|
|
|
login,
|
|
|
|
returnUrl,
|
|
|
|
isRelogin: true,
|
|
|
|
},
|
|
|
|
});
|
2018-02-18 01:29:35 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
browserHistory.push('/login');
|
|
|
|
};
|
2018-02-18 01:29:35 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
function requestTotp({
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
rememberMe,
|
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
login: string;
|
|
|
|
password: string;
|
|
|
|
rememberMe: boolean;
|
|
|
|
}): ThunkAction {
|
|
|
|
return (dispatch, getState) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
// merging with current credentials to propogate returnUrl
|
|
|
|
const credentials = getCredentials(getState());
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: SET_CREDENTIALS,
|
|
|
|
payload: {
|
|
|
|
...credentials,
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
rememberMe,
|
|
|
|
isTotpRequired: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2016-11-13 02:01:44 +05:30
|
|
|
}
|
|
|
|
|
2016-11-13 20:17:56 +05:30
|
|
|
export const SET_SWITCHER = 'auth:setAccountSwitcher';
|
2019-11-27 14:33:32 +05:30
|
|
|
export function setAccountSwitcher(isOn: boolean) {
|
|
|
|
return {
|
|
|
|
type: SET_SWITCHER,
|
|
|
|
payload: isOn,
|
|
|
|
};
|
2016-11-13 20:17:56 +05:30
|
|
|
}
|
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
export const ERROR = 'auth:error';
|
2019-12-07 16:58:52 +05:30
|
|
|
export function setErrors(errors: { [key: string]: ValidationError } | null) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
type: ERROR,
|
|
|
|
payload: errors,
|
|
|
|
error: true,
|
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function clearErrors() {
|
2019-11-27 14:33:32 +05:30
|
|
|
return setErrors(null);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2017-10-17 23:20:34 +05:30
|
|
|
const KNOWN_SCOPES = [
|
2019-11-27 14:33:32 +05:30
|
|
|
'minecraft_server_session',
|
|
|
|
'offline_access',
|
|
|
|
'account_info',
|
|
|
|
'account_email',
|
2017-10-17 23:20:34 +05:30
|
|
|
];
|
2016-08-23 10:00:06 +05:30
|
|
|
/**
|
|
|
|
* @param {object} oauthData
|
|
|
|
* @param {string} oauthData.clientId
|
|
|
|
* @param {string} oauthData.redirectUrl
|
|
|
|
* @param {string} oauthData.responseType
|
|
|
|
* @param {string} oauthData.description
|
|
|
|
* @param {string} oauthData.scope
|
2016-11-19 20:11:15 +05:30
|
|
|
* @param {string} [oauthData.prompt='none'] - comma-separated list of values to adjust auth flow
|
|
|
|
* Posible values:
|
|
|
|
* * none - default behaviour
|
|
|
|
* * consent - forcibly prompt user for rules acceptance
|
|
|
|
* * select_account - force account choosage, even if user has only one
|
|
|
|
* @param {string} oauthData.loginHint - allows to choose the account, which will be used for auth
|
|
|
|
* The possible values: account id, email, username
|
2016-08-23 10:00:06 +05:30
|
|
|
* @param {string} oauthData.state
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Promise}
|
2016-08-23 10:00:06 +05:30
|
|
|
*/
|
2018-05-03 10:45:09 +05:30
|
|
|
export function oAuthValidate(oauthData: OauthData) {
|
2019-11-27 14:33:32 +05:30
|
|
|
// TODO: move to oAuth actions?
|
|
|
|
// test request: /oauth?client_id=ely&redirect_uri=http%3A%2F%2Fely.by&response_type=code&scope=minecraft_server_session&description=foo
|
|
|
|
return wrapInLoader(dispatch =>
|
|
|
|
oauth
|
|
|
|
.validate(oauthData)
|
|
|
|
.then(resp => {
|
|
|
|
const { scopes } = resp.session;
|
|
|
|
const invalidScopes = scopes.filter(
|
|
|
|
scope => !KNOWN_SCOPES.includes(scope),
|
|
|
|
);
|
|
|
|
let prompt = (oauthData.prompt || 'none')
|
|
|
|
.split(',')
|
2019-12-07 16:58:52 +05:30
|
|
|
.map(item => item.trim());
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
if (prompt.includes('none')) {
|
|
|
|
prompt = ['none'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalidScopes.length) {
|
|
|
|
logger.error('Got invalid scopes after oauth validation', {
|
|
|
|
invalidScopes,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(setClient(resp.client));
|
|
|
|
dispatch(
|
|
|
|
setOAuthRequest({
|
|
|
|
...resp.oAuth,
|
|
|
|
prompt: oauthData.prompt || 'none',
|
|
|
|
loginHint: oauthData.loginHint,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
dispatch(setScopes(scopes));
|
|
|
|
localStorage.setItem(
|
|
|
|
'oauthData',
|
|
|
|
JSON.stringify({
|
|
|
|
// @see services/authFlow/AuthFlow
|
|
|
|
timestamp: Date.now(),
|
|
|
|
payload: oauthData,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(handleOauthParamsValidation),
|
|
|
|
);
|
2016-02-27 16:23:58 +05:30
|
|
|
}
|
|
|
|
|
2016-08-27 15:49:02 +05:30
|
|
|
/**
|
|
|
|
* @param {object} params
|
|
|
|
* @param {bool} params.accept=false
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Promise}
|
2016-08-27 15:49:02 +05:30
|
|
|
*/
|
2019-11-27 14:33:32 +05:30
|
|
|
export function oAuthComplete(params: { accept?: boolean } = {}) {
|
2019-12-07 16:58:52 +05:30
|
|
|
return wrapInLoader(
|
|
|
|
async (
|
|
|
|
dispatch,
|
|
|
|
getState,
|
|
|
|
): Promise<{
|
|
|
|
success: boolean;
|
|
|
|
redirectUri: string;
|
|
|
|
}> => {
|
|
|
|
const oauthData = getState().auth.oauth;
|
|
|
|
|
|
|
|
if (!oauthData) {
|
|
|
|
throw new Error('Can not complete oAuth. Oauth data does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const resp = await oauth.complete(oauthData, params);
|
2019-11-27 14:33:32 +05:30
|
|
|
localStorage.removeItem('oauthData');
|
|
|
|
|
|
|
|
if (resp.redirectUri.startsWith('static_page')) {
|
|
|
|
const displayCode = resp.redirectUri === 'static_page_with_code';
|
2019-12-07 16:58:52 +05:30
|
|
|
|
|
|
|
const [, code] = resp.redirectUri.match(/code=(.+)&/) || [];
|
|
|
|
[, resp.redirectUri] = resp.redirectUri.match(/^(.+)\?/) || [];
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
dispatch(
|
|
|
|
setOAuthCode({
|
|
|
|
success: resp.success,
|
|
|
|
code,
|
|
|
|
displayCode,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
2019-12-07 16:58:52 +05:30
|
|
|
} catch (error) {
|
|
|
|
const resp:
|
2019-11-27 14:33:32 +05:30
|
|
|
| {
|
2019-12-07 16:58:52 +05:30
|
|
|
acceptRequired: boolean;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
|
|
|
| {
|
2019-12-07 16:58:52 +05:30
|
|
|
unauthorized: boolean;
|
|
|
|
} = error;
|
|
|
|
|
|
|
|
if ('acceptRequired' in resp) {
|
2019-11-27 14:33:32 +05:30
|
|
|
dispatch(requirePermissionsAccept());
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleOauthParamsValidation(resp);
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
);
|
2016-02-27 16:23:58 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
function handleOauthParamsValidation(
|
2019-12-07 16:58:52 +05:30
|
|
|
resp: {
|
|
|
|
[key: string]: any;
|
|
|
|
userMessage?: string;
|
|
|
|
} = {},
|
2019-11-27 14:33:32 +05:30
|
|
|
) {
|
|
|
|
dispatchBsod();
|
|
|
|
localStorage.removeItem('oauthData');
|
2016-08-07 20:20:00 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
resp.userMessage && setTimeout(() => alert(resp.userMessage), 500); // 500 ms to allow re-render
|
2016-07-27 23:57:21 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return Promise.reject(resp);
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export const SET_CLIENT = 'set_client';
|
2019-11-27 14:33:32 +05:30
|
|
|
export function setClient({ id, name, description }: Client) {
|
|
|
|
return {
|
|
|
|
type: SET_CLIENT,
|
|
|
|
payload: { id, name, description },
|
|
|
|
};
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export function resetOAuth(): ThunkAction {
|
|
|
|
return (dispatch): void => {
|
2019-11-27 14:33:32 +05:30
|
|
|
localStorage.removeItem('oauthData');
|
|
|
|
dispatch(setOAuthRequest({}));
|
|
|
|
};
|
2016-11-19 16:24:24 +05:30
|
|
|
}
|
|
|
|
|
2017-02-01 11:55:15 +05:30
|
|
|
/**
|
|
|
|
* Resets all temporary state related to auth
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
export function resetAuth(): ThunkAction {
|
|
|
|
return (dispatch, getSate): Promise<void> => {
|
2019-11-27 14:33:32 +05:30
|
|
|
dispatch(setLogin(null));
|
|
|
|
dispatch(resetOAuth());
|
|
|
|
// ensure current account is valid
|
|
|
|
const activeAccount = getActiveAccount(getSate());
|
|
|
|
|
|
|
|
if (activeAccount) {
|
2019-12-07 16:58:52 +05:30
|
|
|
return Promise.resolve(dispatch(authenticate(activeAccount)))
|
|
|
|
.then(() => {})
|
|
|
|
.catch(() => {
|
2019-11-27 14:33:32 +05:30
|
|
|
// its okay. user will be redirected to an appropriate place
|
2019-12-07 16:58:52 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2018-03-01 03:28:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return Promise.resolve();
|
|
|
|
};
|
2017-02-01 11:55:15 +05:30
|
|
|
}
|
|
|
|
|
2016-02-27 16:23:58 +05:30
|
|
|
export const SET_OAUTH = 'set_oauth';
|
2017-08-23 00:09:08 +05:30
|
|
|
export function setOAuthRequest(oauth: {
|
2019-12-07 16:58:52 +05:30
|
|
|
client_id?: string;
|
|
|
|
redirect_uri?: string;
|
|
|
|
response_type?: string;
|
|
|
|
scope?: string;
|
|
|
|
prompt?: string;
|
|
|
|
loginHint?: string;
|
|
|
|
state?: string;
|
2017-08-23 00:09:08 +05:30
|
|
|
}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
type: SET_OAUTH,
|
|
|
|
payload: {
|
|
|
|
clientId: oauth.client_id,
|
|
|
|
redirectUrl: oauth.redirect_uri,
|
|
|
|
responseType: oauth.response_type,
|
|
|
|
scope: oauth.scope,
|
|
|
|
prompt: oauth.prompt,
|
|
|
|
loginHint: oauth.loginHint,
|
|
|
|
state: oauth.state,
|
|
|
|
},
|
|
|
|
};
|
2016-02-27 16:23:58 +05:30
|
|
|
}
|
2016-02-29 23:46:33 +05:30
|
|
|
|
2016-03-15 12:06:13 +05:30
|
|
|
export const SET_OAUTH_RESULT = 'set_oauth_result';
|
2017-08-23 00:09:08 +05:30
|
|
|
export function setOAuthCode(oauth: {
|
2019-12-07 16:58:52 +05:30
|
|
|
success: boolean;
|
|
|
|
code: string;
|
|
|
|
displayCode: boolean;
|
2017-08-23 00:09:08 +05:30
|
|
|
}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
type: SET_OAUTH_RESULT,
|
|
|
|
payload: {
|
|
|
|
success: oauth.success,
|
|
|
|
code: oauth.code,
|
|
|
|
displayCode: oauth.displayCode,
|
|
|
|
},
|
|
|
|
};
|
2016-03-15 12:06:13 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 01:24:35 +05:30
|
|
|
export const REQUIRE_PERMISSIONS_ACCEPT = 'require_permissions_accept';
|
|
|
|
export function requirePermissionsAccept() {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
type: REQUIRE_PERMISSIONS_ACCEPT,
|
|
|
|
};
|
2016-04-15 01:24:35 +05:30
|
|
|
}
|
|
|
|
|
2016-02-29 23:46:33 +05:30
|
|
|
export const SET_SCOPES = 'set_scopes';
|
2019-06-30 19:02:50 +05:30
|
|
|
export function setScopes(scopes: Scope[]) {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!(scopes instanceof Array)) {
|
|
|
|
throw new Error('Scopes must be array');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: SET_SCOPES,
|
|
|
|
payload: scopes,
|
|
|
|
};
|
2016-02-29 23:46:33 +05:30
|
|
|
}
|
2016-04-02 16:28:54 +05:30
|
|
|
|
|
|
|
export const SET_LOADING_STATE = 'set_loading_state';
|
2019-11-27 14:33:32 +05:30
|
|
|
export function setLoadingState(isLoading: boolean) {
|
|
|
|
return {
|
|
|
|
type: SET_LOADING_STATE,
|
|
|
|
payload: isLoading,
|
|
|
|
};
|
2016-04-02 16:28:54 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
function wrapInLoader<T>(fn: ThunkAction<Promise<T>>): ThunkAction<Promise<T>> {
|
|
|
|
return (dispatch, getState) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
dispatch(setLoadingState(true));
|
|
|
|
const endLoading = () => dispatch(setLoadingState(false));
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
return fn(dispatch, getState, undefined).then(
|
2019-11-27 14:33:32 +05:30
|
|
|
resp => {
|
|
|
|
endLoading();
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return resp;
|
|
|
|
},
|
|
|
|
resp => {
|
|
|
|
endLoading();
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return Promise.reject(resp);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
2016-04-02 16:28:54 +05:30
|
|
|
}
|
2016-04-15 09:39:04 +05:30
|
|
|
|
|
|
|
function needActivation() {
|
2019-11-27 14:33:32 +05:30
|
|
|
return updateUser({
|
|
|
|
isActive: false,
|
|
|
|
isGuest: false,
|
|
|
|
});
|
2016-04-15 09:39:04 +05:30
|
|
|
}
|
2016-05-15 02:23:58 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
function authHandler(dispatch: Dispatch) {
|
|
|
|
return (resp: OAuthResponse): Promise<Account> =>
|
2019-11-27 14:33:32 +05:30
|
|
|
dispatch(
|
|
|
|
authenticate({
|
2016-11-05 15:41:41 +05:30
|
|
|
token: resp.access_token,
|
2019-12-07 16:58:52 +05:30
|
|
|
refreshToken: resp.refresh_token || null,
|
2019-11-27 14:33:32 +05:30
|
|
|
}),
|
|
|
|
).then(resp => {
|
|
|
|
dispatch(setLogin(null));
|
2016-11-13 02:01:44 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return resp;
|
2016-11-13 02:01:44 +05:30
|
|
|
});
|
2016-06-04 19:16:39 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
function validationErrorsHandler(dispatch: Dispatch, repeatUrl?: string) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return resp => {
|
|
|
|
if (resp.errors) {
|
|
|
|
const firstError = Object.keys(resp.errors)[0];
|
|
|
|
const error = {
|
|
|
|
type: resp.errors[firstError],
|
|
|
|
payload: {
|
|
|
|
isGuest: true,
|
|
|
|
repeatUrl: '',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (resp.data) {
|
|
|
|
// TODO: this should be formatted on backend
|
|
|
|
Object.assign(error.payload, resp.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
['error.key_not_exists', 'error.key_expire'].includes(error.type) &&
|
|
|
|
repeatUrl
|
|
|
|
) {
|
|
|
|
// TODO: this should be formatted on backend
|
|
|
|
error.payload.repeatUrl = repeatUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.errors[firstError] = error;
|
|
|
|
|
|
|
|
dispatch(setErrors(resp.errors));
|
|
|
|
}
|
2016-05-15 02:23:58 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return Promise.reject(resp);
|
|
|
|
};
|
2016-05-15 02:23:58 +05:30
|
|
|
}
|