2017-08-23 00:09:08 +05:30
|
|
|
// @flow
|
2017-05-26 00:41:57 +05:30
|
|
|
import { browserHistory } from 'services/history';
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2017-01-04 11:22:46 +05:30
|
|
|
import logger from 'services/logger';
|
2017-04-12 00:48:27 +05:30
|
|
|
import localStorage from 'services/localStorage';
|
2017-02-01 11:52:12 +05:30
|
|
|
import loader from 'services/loader';
|
2017-01-29 21:06:21 +05:30
|
|
|
import history from 'services/history';
|
2017-01-04 11:22:46 +05:30
|
|
|
import { updateUser, acceptRules as userAcceptRules } from 'components/user/actions';
|
|
|
|
import { authenticate, logoutAll } from 'components/accounts/actions';
|
2016-06-04 19:16:39 +05:30
|
|
|
import authentication from 'services/api/authentication';
|
2016-07-27 23:57:21 +05:30
|
|
|
import oauth from 'services/api/oauth';
|
2016-07-28 10:33:30 +05:30
|
|
|
import signup from 'services/api/signup';
|
2016-08-07 20:20:00 +05:30
|
|
|
import dispatchBsod from 'components/ui/bsod/dispatchBsod';
|
2017-02-24 02:37:05 +05:30
|
|
|
import { create as createPopup } from 'components/ui/popup/actions';
|
|
|
|
import ContactForm from 'components/contact/ContactForm';
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2017-01-04 11:22:46 +05:30
|
|
|
export { updateUser } from 'components/user/actions';
|
|
|
|
export { authenticate, logoutAll as logout } from 'components/accounts/actions';
|
|
|
|
|
2017-01-29 21:06:21 +05:30
|
|
|
/**
|
|
|
|
* Reoutes user to the previous page if it is possible
|
|
|
|
*
|
|
|
|
* @param {string} fallbackUrl - an url to route user to if goBack is not possible
|
|
|
|
*
|
|
|
|
* @return {object} - action definition
|
|
|
|
*/
|
2017-08-23 00:09:08 +05:30
|
|
|
export function goBack(fallbackUrl?: ?string = null) {
|
2017-01-29 21:06:21 +05:30
|
|
|
if (history.canGoBack()) {
|
2017-05-09 01:04:50 +05:30
|
|
|
browserHistory.goBack();
|
2017-01-29 21:06:21 +05:30
|
|
|
} else if (fallbackUrl) {
|
2017-05-09 01:04:50 +05:30
|
|
|
browserHistory.push(fallbackUrl);
|
2017-01-29 21:06:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'noop'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-09 19:52:19 +05:30
|
|
|
export function redirect(url: string): () => Promise<*> {
|
2017-02-01 11:52:12 +05:30
|
|
|
loader.show();
|
|
|
|
|
|
|
|
return () => new Promise(() => {
|
|
|
|
// do not resolve promise to make loader visible and
|
|
|
|
// overcome app rendering
|
|
|
|
location.href = url;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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({
|
|
|
|
login = '',
|
|
|
|
password = '',
|
|
|
|
totp,
|
|
|
|
rememberMe = false
|
|
|
|
}: {
|
|
|
|
login: string,
|
|
|
|
password?: string,
|
|
|
|
totp?: string,
|
|
|
|
rememberMe?: bool
|
|
|
|
}) {
|
2016-04-02 16:28:54 +05:30
|
|
|
return wrapInLoader((dispatch) =>
|
2016-06-04 19:16:39 +05:30
|
|
|
authentication.login(
|
2017-08-23 00:09:08 +05:30
|
|
|
{login, password, totp, rememberMe}
|
2016-02-13 20:58:47 +05:30
|
|
|
)
|
2017-08-23 02:30:10 +05:30
|
|
|
.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());
|
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2016-02-26 23:43:41 +05:30
|
|
|
|
2017-10-17 22:16:00 +05:30
|
|
|
return Promise.reject(resp);
|
2017-08-23 02:30:10 +05:30
|
|
|
})
|
2017-10-17 22:16:00 +05:30
|
|
|
.catch(validationErrorsHandler(dispatch))
|
2016-04-02 16:28:54 +05:30
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2016-08-03 00:29:29 +05:30
|
|
|
export function acceptRules() {
|
|
|
|
return wrapInLoader((dispatch) =>
|
|
|
|
dispatch(userAcceptRules())
|
|
|
|
.catch(validationErrorsHandler(dispatch))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-15 02:23:58 +05:30
|
|
|
export function forgotPassword({
|
2017-04-18 22:17:46 +05:30
|
|
|
login = '',
|
|
|
|
captcha = ''
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
|
|
|
login: string,
|
|
|
|
captcha: string
|
2016-05-15 02:23:58 +05:30
|
|
|
}) {
|
|
|
|
return wrapInLoader((dispatch, getState) =>
|
2017-04-18 22:17:46 +05:30
|
|
|
authentication.forgotPassword({login, captcha})
|
2016-07-28 10:33:30 +05:30
|
|
|
.then(({data = {}}) => dispatch(updateUser({
|
|
|
|
maskedEmail: data.emailMask || getState().user.email
|
|
|
|
})))
|
|
|
|
.catch(validationErrorsHandler(dispatch))
|
2016-05-15 02:23:58 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function recoverPassword({
|
|
|
|
key = '',
|
|
|
|
newPassword = '',
|
|
|
|
newRePassword = ''
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
|
|
|
key: string,
|
|
|
|
newPassword: string,
|
|
|
|
newRePassword: string
|
2016-05-15 02:23:58 +05:30
|
|
|
}) {
|
|
|
|
return wrapInLoader((dispatch) =>
|
2016-07-28 10:33:30 +05:30
|
|
|
authentication.recoverPassword({key, newPassword, newRePassword})
|
|
|
|
.then(authHandler(dispatch))
|
|
|
|
.catch(validationErrorsHandler(dispatch, '/forgot-password'))
|
2016-04-02 16:28:54 +05:30
|
|
|
);
|
2016-03-16 11:24:42 +05:30
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
export function register({
|
|
|
|
email = '',
|
|
|
|
username = '',
|
|
|
|
password = '',
|
|
|
|
rePassword = '',
|
2016-08-05 11:13:46 +05:30
|
|
|
captcha = '',
|
2016-02-13 20:58:47 +05:30
|
|
|
rulesAgreement = false
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
|
|
|
email: string,
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
rePassword: string,
|
|
|
|
captcha: string,
|
|
|
|
rulesAgreement: bool
|
2016-02-13 20:58:47 +05:30
|
|
|
}) {
|
2016-05-14 18:30:14 +05:30
|
|
|
return wrapInLoader((dispatch, getState) =>
|
2016-07-28 10:33:30 +05:30
|
|
|
signup.register({
|
|
|
|
email, username,
|
|
|
|
password, rePassword,
|
2016-08-05 11:13:46 +05:30
|
|
|
rulesAgreement, lang: getState().user.lang,
|
|
|
|
captcha
|
2016-07-28 10:33:30 +05:30
|
|
|
})
|
2017-08-23 02:30:10 +05:30
|
|
|
.then(() => {
|
|
|
|
dispatch(updateUser({
|
|
|
|
username,
|
|
|
|
email
|
|
|
|
}));
|
2017-05-09 01:04:50 +05:30
|
|
|
|
2017-08-23 02:30:10 +05:30
|
|
|
dispatch(needActivation());
|
2017-05-09 01:04:50 +05:30
|
|
|
|
2017-08-23 02:30:10 +05:30
|
|
|
browserHistory.push('/activation');
|
|
|
|
})
|
|
|
|
.catch(validationErrorsHandler(dispatch))
|
2016-04-02 16:28:54 +05:30
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
export function activate({key = ''}: {key: string}) {
|
2016-04-02 16:28:54 +05:30
|
|
|
return wrapInLoader((dispatch) =>
|
2016-07-28 10:33:30 +05:30
|
|
|
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({
|
|
|
|
email = '',
|
|
|
|
captcha
|
|
|
|
}: {
|
|
|
|
email: string,
|
|
|
|
captcha: string
|
|
|
|
}) {
|
2016-05-23 00:28:43 +05:30
|
|
|
return wrapInLoader((dispatch) =>
|
2016-07-31 19:23:16 +05:30
|
|
|
signup.resendActivation({email, captcha})
|
2016-07-28 10:33:30 +05:30
|
|
|
.then((resp) => {
|
|
|
|
dispatch(updateUser({
|
|
|
|
email
|
|
|
|
}));
|
2016-07-23 18:13:37 +05:30
|
|
|
|
2016-07-28 10:33:30 +05:30
|
|
|
return resp;
|
|
|
|
})
|
|
|
|
.catch(validationErrorsHandler(dispatch))
|
2016-04-02 16:28:54 +05:30
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2017-02-24 02:37:05 +05:30
|
|
|
export function contactUs() {
|
|
|
|
return createPopup(ContactForm);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return {object}
|
|
|
|
*/
|
|
|
|
export function setLogin(login: ?string) {
|
|
|
|
return {
|
|
|
|
type: SET_CREDENTIALS,
|
|
|
|
payload: login ? {
|
|
|
|
login
|
|
|
|
} : null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestTotp({login, password, rememberMe}: {
|
|
|
|
login: string,
|
|
|
|
password: string,
|
|
|
|
rememberMe: bool
|
|
|
|
}) {
|
2016-11-13 02:01:44 +05:30
|
|
|
return {
|
2017-08-23 00:09:08 +05:30
|
|
|
type: SET_CREDENTIALS,
|
|
|
|
payload: {
|
|
|
|
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';
|
2017-08-23 00:09:08 +05:30
|
|
|
export function setAccountSwitcher(isOn: bool) {
|
2016-11-13 20:17:56 +05:30
|
|
|
return {
|
|
|
|
type: SET_SWITCHER,
|
|
|
|
payload: isOn
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
export const ERROR = 'auth:error';
|
2017-08-23 00:09:08 +05:30
|
|
|
export function setErrors(errors: ?{[key: string]: string}) {
|
2016-02-13 20:58:47 +05:30
|
|
|
return {
|
|
|
|
type: ERROR,
|
2016-08-23 00:48:11 +05:30
|
|
|
payload: errors,
|
2016-02-13 20:58:47 +05:30
|
|
|
error: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearErrors() {
|
2016-08-23 00:48:11 +05:30
|
|
|
return setErrors(null);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2017-10-17 23:20:34 +05:30
|
|
|
const KNOWN_SCOPES = [
|
|
|
|
'minecraft_server_session',
|
|
|
|
'offline_access',
|
|
|
|
'account_info',
|
|
|
|
'account_email',
|
|
|
|
];
|
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
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-08-23 00:09:08 +05:30
|
|
|
export function oAuthValidate(oauthData: {
|
|
|
|
clientId: string,
|
|
|
|
redirectUrl: string,
|
|
|
|
responseType: string,
|
|
|
|
description: string,
|
|
|
|
scope: string,
|
2017-09-09 20:34:26 +05:30
|
|
|
prompt: 'none' | 'consent' | 'select_account',
|
2017-08-23 00:09:08 +05:30
|
|
|
loginHint?: string,
|
|
|
|
state?: string
|
|
|
|
}) {
|
2016-08-23 10:00:06 +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
|
2016-04-02 16:28:54 +05:30
|
|
|
return wrapInLoader((dispatch) =>
|
2016-07-27 23:57:21 +05:30
|
|
|
oauth.validate(oauthData)
|
|
|
|
.then((resp) => {
|
2017-10-17 23:20:34 +05:30
|
|
|
const scopes = resp.session.scopes;
|
|
|
|
const invalidScopes = scopes.filter((scope) => !KNOWN_SCOPES.includes(scope));
|
2016-11-19 20:11:15 +05:30
|
|
|
let prompt = (oauthData.prompt || 'none').split(',').map((item) => item.trim);
|
2017-10-17 23:20:34 +05:30
|
|
|
|
2016-11-19 20:11:15 +05:30
|
|
|
if (prompt.includes('none')) {
|
|
|
|
prompt = ['none'];
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:20:34 +05:30
|
|
|
if (invalidScopes.length) {
|
|
|
|
logger.error('Got invalid scopes after oauth validation', {
|
|
|
|
invalidScopes
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-27 23:57:21 +05:30
|
|
|
dispatch(setClient(resp.client));
|
2016-11-19 20:11:15 +05:30
|
|
|
dispatch(setOAuthRequest({
|
|
|
|
...resp.oAuth,
|
|
|
|
prompt: oauthData.prompt || 'none',
|
|
|
|
loginHint: oauthData.loginHint
|
|
|
|
}));
|
2017-10-17 23:20:34 +05:30
|
|
|
dispatch(setScopes(scopes));
|
2016-08-12 00:50:14 +05:30
|
|
|
localStorage.setItem('oauthData', JSON.stringify({ // @see services/authFlow/AuthFlow
|
|
|
|
timestamp: Date.now(),
|
|
|
|
payload: oauthData
|
|
|
|
}));
|
2016-07-27 23:57:21 +05:30
|
|
|
})
|
|
|
|
.catch(handleOauthParamsValidation)
|
2016-04-02 16:28:54 +05:30
|
|
|
);
|
2016-02-27 16:23:58 +05:30
|
|
|
}
|
|
|
|
|
2016-08-27 15:49:02 +05:30
|
|
|
/**
|
|
|
|
* @param {object} params
|
|
|
|
* @param {bool} params.accept=false
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2017-08-23 00:09:08 +05:30
|
|
|
export function oAuthComplete(params: {accept?: bool} = {}) {
|
2016-07-27 23:57:21 +05:30
|
|
|
return wrapInLoader((dispatch, getState) =>
|
|
|
|
oauth.complete(getState().auth.oauth, params)
|
|
|
|
.then((resp) => {
|
2016-08-27 15:49:02 +05:30
|
|
|
localStorage.removeItem('oauthData');
|
|
|
|
|
2016-07-27 23:57:21 +05:30
|
|
|
if (resp.redirectUri.startsWith('static_page')) {
|
|
|
|
resp.code = resp.redirectUri.match(/code=(.+)&/)[1];
|
|
|
|
resp.redirectUri = resp.redirectUri.match(/^(.+)\?/)[1];
|
|
|
|
resp.displayCode = resp.redirectUri === 'static_page_with_code';
|
|
|
|
|
|
|
|
dispatch(setOAuthCode({
|
|
|
|
success: resp.success,
|
|
|
|
code: resp.code,
|
|
|
|
displayCode: resp.displayCode
|
|
|
|
}));
|
|
|
|
}
|
2016-03-15 12:06:13 +05:30
|
|
|
|
2016-07-27 23:57:21 +05:30
|
|
|
return resp;
|
|
|
|
}, (resp) => {
|
|
|
|
if (resp.acceptRequired) {
|
|
|
|
dispatch(requirePermissionsAccept());
|
2016-02-27 16:23:58 +05:30
|
|
|
|
2016-08-07 20:20:00 +05:30
|
|
|
return Promise.reject(resp);
|
|
|
|
}
|
2016-08-23 10:00:06 +05:30
|
|
|
|
|
|
|
return handleOauthParamsValidation(resp);
|
2016-07-27 23:57:21 +05:30
|
|
|
})
|
|
|
|
);
|
2016-02-27 16:23:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function handleOauthParamsValidation(resp = {}) {
|
2016-08-07 20:20:00 +05:30
|
|
|
dispatchBsod();
|
2016-08-27 15:49:02 +05:30
|
|
|
localStorage.removeItem('oauthData');
|
2016-08-07 20:20:00 +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
|
|
|
|
|
|
|
return Promise.reject(resp);
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export const SET_CLIENT = 'set_client';
|
2017-08-23 00:09:08 +05:30
|
|
|
export function setClient({
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
description
|
|
|
|
}: {
|
|
|
|
id: string,
|
|
|
|
name: string,
|
|
|
|
description: string
|
|
|
|
}) {
|
2016-02-23 11:27:16 +05:30
|
|
|
return {
|
|
|
|
type: SET_CLIENT,
|
|
|
|
payload: {id, name, description}
|
|
|
|
};
|
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
|
2016-11-19 16:24:24 +05:30
|
|
|
export function resetOAuth() {
|
2017-09-09 20:34:26 +05:30
|
|
|
return (dispatch: (Function | Object) => void) => {
|
2016-11-19 16:24:24 +05:30
|
|
|
localStorage.removeItem('oauthData');
|
|
|
|
dispatch(setOAuthRequest({}));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-01 11:55:15 +05:30
|
|
|
/**
|
|
|
|
* Resets all temporary state related to auth
|
|
|
|
*
|
|
|
|
* @return {function}
|
|
|
|
*/
|
|
|
|
export function resetAuth() {
|
2017-09-09 20:34:26 +05:30
|
|
|
return (dispatch: (Function | Object) => void) => {
|
2017-02-01 11:55:15 +05:30
|
|
|
dispatch(setLogin(null));
|
2017-08-23 00:09:08 +05:30
|
|
|
dispatch(resetOAuth());
|
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: {
|
|
|
|
client_id?: string,
|
|
|
|
redirect_uri?: string,
|
|
|
|
response_type?: string,
|
|
|
|
scope?: string,
|
|
|
|
prompt?: string,
|
|
|
|
loginHint?: string,
|
|
|
|
state?: string
|
|
|
|
}) {
|
2016-02-27 16:23:58 +05:30
|
|
|
return {
|
|
|
|
type: SET_OAUTH,
|
|
|
|
payload: {
|
|
|
|
clientId: oauth.client_id,
|
|
|
|
redirectUrl: oauth.redirect_uri,
|
|
|
|
responseType: oauth.response_type,
|
|
|
|
scope: oauth.scope,
|
2016-11-19 20:11:15 +05:30
|
|
|
prompt: oauth.prompt,
|
|
|
|
loginHint: oauth.loginHint,
|
2016-02-27 16:23:58 +05:30
|
|
|
state: oauth.state
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
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: {
|
|
|
|
success: bool,
|
|
|
|
code: string,
|
|
|
|
displayCode: bool
|
|
|
|
}) {
|
2016-03-15 12:06:13 +05:30
|
|
|
return {
|
|
|
|
type: SET_OAUTH_RESULT,
|
|
|
|
payload: {
|
|
|
|
success: oauth.success,
|
|
|
|
code: oauth.code,
|
|
|
|
displayCode: oauth.displayCode
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-15 01:24:35 +05:30
|
|
|
export const REQUIRE_PERMISSIONS_ACCEPT = 'require_permissions_accept';
|
|
|
|
export function requirePermissionsAccept() {
|
|
|
|
return {
|
|
|
|
type: REQUIRE_PERMISSIONS_ACCEPT
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-29 23:46:33 +05:30
|
|
|
export const SET_SCOPES = 'set_scopes';
|
2017-08-23 00:09:08 +05:30
|
|
|
export function setScopes(scopes: Array<string>) {
|
2016-02-29 23:46:33 +05:30
|
|
|
if (!(scopes instanceof Array)) {
|
|
|
|
throw new Error('Scopes must be array');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: SET_SCOPES,
|
|
|
|
payload: scopes
|
|
|
|
};
|
|
|
|
}
|
2016-04-02 16:28:54 +05:30
|
|
|
|
|
|
|
|
|
|
|
export const SET_LOADING_STATE = 'set_loading_state';
|
2017-08-23 00:09:08 +05:30
|
|
|
export function setLoadingState(isLoading: bool) {
|
2016-04-02 16:28:54 +05:30
|
|
|
return {
|
|
|
|
type: SET_LOADING_STATE,
|
|
|
|
payload: isLoading
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapInLoader(fn) {
|
2017-09-09 20:34:26 +05:30
|
|
|
return (dispatch: (Function | Object) => void, getState: Object) => {
|
2016-04-02 16:28:54 +05:30
|
|
|
dispatch(setLoadingState(true));
|
|
|
|
const endLoading = () => dispatch(setLoadingState(false));
|
|
|
|
|
|
|
|
return Reflect.apply(fn, null, [dispatch, getState]).then((resp) => {
|
|
|
|
endLoading();
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}, (resp) => {
|
|
|
|
endLoading();
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2016-04-15 09:39:04 +05:30
|
|
|
|
|
|
|
function needActivation() {
|
|
|
|
return updateUser({
|
|
|
|
isActive: false,
|
|
|
|
isGuest: false
|
|
|
|
});
|
|
|
|
}
|
2016-05-15 02:23:58 +05:30
|
|
|
|
2016-06-04 19:16:39 +05:30
|
|
|
function authHandler(dispatch) {
|
2016-11-05 15:41:41 +05:30
|
|
|
return (resp) => dispatch(authenticate({
|
|
|
|
token: resp.access_token,
|
|
|
|
refreshToken: resp.refresh_token
|
2016-11-13 02:01:44 +05:30
|
|
|
})).then((resp) => {
|
|
|
|
dispatch(setLogin(null));
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
});
|
2016-06-04 19:16:39 +05:30
|
|
|
}
|
|
|
|
|
2017-10-17 22:16:00 +05:30
|
|
|
function validationErrorsHandler(
|
|
|
|
dispatch: (Function | Object) => void,
|
|
|
|
repeatUrl?: string
|
|
|
|
) {
|
2016-05-15 02:23:58 +05:30
|
|
|
return (resp) => {
|
|
|
|
if (resp.errors) {
|
2016-08-23 00:48:11 +05:30
|
|
|
const firstError = Object.keys(resp.errors)[0];
|
2016-05-22 20:46:51 +05:30
|
|
|
const error = {
|
2016-08-23 00:48:11 +05:30
|
|
|
type: resp.errors[firstError],
|
2016-05-22 20:46:51 +05:30
|
|
|
payload: {
|
2017-08-23 00:09:08 +05:30
|
|
|
isGuest: true,
|
|
|
|
repeatUrl: ''
|
2016-05-22 20:46:51 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-22 19:31:31 +05:30
|
|
|
if (resp.data) {
|
2016-08-23 00:48:11 +05:30
|
|
|
// TODO: this should be formatted on backend
|
2016-05-22 20:46:51 +05:30
|
|
|
Object.assign(error.payload, resp.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['error.key_not_exists', 'error.key_expire'].includes(error.type) && repeatUrl) {
|
2016-08-23 00:48:11 +05:30
|
|
|
// TODO: this should be formatted on backend
|
2017-08-23 00:09:08 +05:30
|
|
|
error.payload.repeatUrl = repeatUrl;
|
2016-05-22 19:31:31 +05:30
|
|
|
}
|
|
|
|
|
2016-08-23 00:48:11 +05:30
|
|
|
resp.errors[firstError] = error;
|
|
|
|
|
|
|
|
dispatch(setErrors(resp.errors));
|
2016-05-15 02:23:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
};
|
|
|
|
}
|