2020-01-18 02:07:52 +05:30
|
|
|
import { Action as ReduxAction } from 'redux';
|
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';
|
2020-01-18 02:07:52 +05:30
|
|
|
import * as loader from 'app/services/loader';
|
2019-12-08 00:32:00 +05:30
|
|
|
import history from 'app/services/history';
|
2020-05-24 04:38:24 +05:30
|
|
|
import { updateUser, acceptRules as userAcceptRules } from 'app/components/user/actions';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { authenticate, logoutAll } from 'app/components/accounts/actions';
|
|
|
|
import { getActiveAccount } from 'app/components/accounts/reducer';
|
2019-01-28 00:42:58 +05:30
|
|
|
import {
|
2020-05-24 04:38:24 +05:30
|
|
|
login as loginEndpoint,
|
|
|
|
forgotPassword as forgotPasswordEndpoint,
|
|
|
|
recoverPassword as recoverPasswordEndpoint,
|
|
|
|
OAuthResponse,
|
2019-12-08 00:32:00 +05:30
|
|
|
} from 'app/services/api/authentication';
|
2024-12-11 01:12:06 +05:30
|
|
|
import oauth, { OauthRequestData, Scope } from 'app/services/api/oauth';
|
2020-01-18 02:07:52 +05:30
|
|
|
import {
|
2020-05-24 04:38:24 +05:30
|
|
|
register as registerEndpoint,
|
|
|
|
activate as activateEndpoint,
|
|
|
|
resendActivation as resendActivationEndpoint,
|
2020-01-18 02:07:52 +05:30
|
|
|
} from 'app/services/api/signup';
|
2019-12-08 00:32:00 +05:30
|
|
|
import dispatchBsod from 'app/components/ui/bsod/dispatchBsod';
|
|
|
|
import { create as createPopup } from 'app/components/ui/popup/actions';
|
2020-07-06 21:59:56 +05:30
|
|
|
import ContactForm from 'app/components/contact';
|
2019-12-26 17:48:58 +05:30
|
|
|
import { Account } from 'app/components/accounts/reducer';
|
2020-07-22 15:31:12 +05:30
|
|
|
import { Action as AppAction, Dispatch } from 'app/types';
|
2020-01-18 02:07:52 +05:30
|
|
|
import { Resp } from 'app/services/request';
|
|
|
|
|
|
|
|
import { Credentials, Client, OAuthState, getCredentials } from './reducer';
|
2019-06-30 19:02:50 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface ValidationErrorLiteral {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: string;
|
|
|
|
payload: Record<string, any>;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
type ValidationError = string | ValidationErrorLiteral;
|
2019-06-30 19:02:50 +05:30
|
|
|
|
2017-01-29 21:06:21 +05:30
|
|
|
/**
|
2019-12-26 17:48:58 +05:30
|
|
|
* Routes user to the previous page if it is possible
|
2017-01-29 21:06:21 +05:30
|
|
|
*
|
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 }) {
|
2020-05-24 04:38:24 +05:30
|
|
|
const { fallbackUrl } = options || {};
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (history.canGoBack()) {
|
|
|
|
browserHistory.goBack();
|
|
|
|
} else if (fallbackUrl) {
|
|
|
|
browserHistory.push(fallbackUrl);
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
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> {
|
2020-05-24 04:38:24 +05:30
|
|
|
loader.show();
|
2017-02-01 11:52:12 +05:30
|
|
|
|
2020-05-24 04:38:24 +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({
|
2020-05-24 04:38:24 +05:30
|
|
|
login = '',
|
|
|
|
password = '',
|
|
|
|
totp,
|
|
|
|
rememberMe = false,
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
2020-05-24 04:38:24 +05:30
|
|
|
login: string;
|
|
|
|
password?: string;
|
|
|
|
totp?: string;
|
|
|
|
rememberMe?: boolean;
|
2017-08-23 00:09:08 +05:30
|
|
|
}) {
|
2020-05-24 04:38:24 +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) {
|
2021-03-28 20:37:46 +05:30
|
|
|
return dispatch(needActivation({ login }));
|
2020-05-24 04:38:24 +05:30
|
|
|
} 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() {
|
2020-05-24 04:38:24 +05:30
|
|
|
return wrapInLoader((dispatch) => dispatch(userAcceptRules()).catch(validationErrorsHandler(dispatch)));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function forgotPassword({ login = '', captcha = '' }: { login: string; captcha: string }) {
|
|
|
|
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({
|
2020-05-24 04:38:24 +05:30
|
|
|
key = '',
|
|
|
|
newPassword = '',
|
|
|
|
newRePassword = '',
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
2020-05-24 04:38:24 +05:30
|
|
|
key: string;
|
|
|
|
newPassword: string;
|
|
|
|
newRePassword: string;
|
2016-05-15 02:23:58 +05:30
|
|
|
}) {
|
2020-05-24 04:38:24 +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({
|
2020-05-24 04:38:24 +05:30
|
|
|
email = '',
|
|
|
|
username = '',
|
|
|
|
password = '',
|
|
|
|
rePassword = '',
|
|
|
|
captcha = '',
|
|
|
|
rulesAgreement = false,
|
2017-08-23 00:09:08 +05:30
|
|
|
}: {
|
2020-05-24 04:38:24 +05:30
|
|
|
email: string;
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
rePassword: string;
|
|
|
|
captcha: string;
|
|
|
|
rulesAgreement: boolean;
|
2016-02-13 20:58:47 +05:30
|
|
|
}) {
|
2020-05-24 04:38:24 +05:30
|
|
|
return wrapInLoader((dispatch, getState) =>
|
|
|
|
registerEndpoint({
|
2019-11-27 14:33:32 +05:30
|
|
|
email,
|
2020-05-24 04:38:24 +05:30
|
|
|
username,
|
|
|
|
password,
|
|
|
|
rePassword,
|
|
|
|
rulesAgreement,
|
|
|
|
lang: getState().user.lang,
|
|
|
|
captcha,
|
|
|
|
})
|
|
|
|
.then(() => {
|
2021-03-28 20:37:46 +05:30
|
|
|
dispatch(needActivation({ login: email || username }));
|
2020-05-24 04:38:24 +05:30
|
|
|
|
|
|
|
browserHistory.push('/activation');
|
|
|
|
})
|
|
|
|
.catch(validationErrorsHandler(dispatch)),
|
|
|
|
);
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2024-08-28 16:37:23 +05:30
|
|
|
export function activate(key: string): AppAction<Promise<Account>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return wrapInLoader((dispatch) =>
|
|
|
|
activateEndpoint(key)
|
|
|
|
.then(authHandler(dispatch))
|
|
|
|
.catch(validationErrorsHandler(dispatch, '/resend-activation')),
|
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
export function resendActivation({ email = '', captcha }: { email: string; captcha: string }) {
|
|
|
|
return wrapInLoader((dispatch) =>
|
|
|
|
resendActivationEndpoint(email, captcha)
|
|
|
|
.then((resp) => {
|
|
|
|
dispatch(
|
|
|
|
updateUser({
|
|
|
|
email,
|
|
|
|
}),
|
|
|
|
);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return resp;
|
|
|
|
})
|
|
|
|
.catch(validationErrorsHandler(dispatch)),
|
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2017-02-24 02:37:05 +05:30
|
|
|
export function contactUs() {
|
2020-05-24 04:38:24 +05:30
|
|
|
return createPopup({ Popup: ContactForm });
|
2017-02-24 02:37:05 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface SetCredentialsAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'auth:setCredentials';
|
|
|
|
payload: Credentials | null;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function setCredentials(payload: Credentials | null): SetCredentialsAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: 'auth:setCredentials',
|
|
|
|
payload,
|
|
|
|
};
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
/**
|
|
|
|
* Sets login in credentials state
|
|
|
|
* Resets the state, when `null` is passed
|
|
|
|
*/
|
2020-01-18 02:07:52 +05:30
|
|
|
export function setLogin(login: string | null): SetCredentialsAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return setCredentials(login ? { login } : null);
|
2017-08-23 00:09:08 +05:30
|
|
|
}
|
|
|
|
|
2020-07-22 15:31:12 +05:30
|
|
|
export function relogin(login: string | null): AppAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
const credentials = getCredentials(getState());
|
|
|
|
const returnUrl = credentials.returnUrl || location.pathname + location.search;
|
2018-02-18 01:29:35 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch(
|
|
|
|
setCredentials({
|
|
|
|
login,
|
|
|
|
returnUrl,
|
|
|
|
isRelogin: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
browserHistory.push('/login');
|
|
|
|
};
|
2018-02-18 01:29:35 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export type CredentialsAction = SetCredentialsAction;
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
function requestTotp({
|
2020-05-24 04:38:24 +05:30
|
|
|
login,
|
|
|
|
password,
|
|
|
|
rememberMe,
|
2019-11-27 14:33:32 +05:30
|
|
|
}: {
|
2020-05-24 04:38:24 +05:30
|
|
|
login: string;
|
|
|
|
password: string;
|
|
|
|
rememberMe: boolean;
|
2020-07-22 15:31:12 +05:30
|
|
|
}): AppAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
// merging with current credentials to propogate returnUrl
|
|
|
|
const credentials = getCredentials(getState());
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
setCredentials({
|
|
|
|
...credentials,
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
rememberMe,
|
|
|
|
isTotpRequired: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
2016-11-13 02:01:44 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface SetSwitcherAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'auth:setAccountSwitcher';
|
|
|
|
payload: boolean;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function setAccountSwitcher(isOn: boolean): SetSwitcherAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: 'auth:setAccountSwitcher',
|
|
|
|
payload: isOn,
|
|
|
|
};
|
2016-11-13 20:17:56 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export type AccountSwitcherAction = SetSwitcherAction;
|
|
|
|
|
|
|
|
interface SetErrorAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'auth:error';
|
|
|
|
payload: Record<string, ValidationError> | null;
|
|
|
|
error: boolean;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
export function setErrors(errors: Record<string, ValidationError> | null): SetErrorAction {
|
|
|
|
return {
|
|
|
|
type: 'auth:error',
|
|
|
|
payload: errors,
|
|
|
|
error: true,
|
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export function clearErrors(): SetErrorAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return setErrors(null);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export type ErrorAction = SetErrorAction;
|
|
|
|
|
|
|
|
const KNOWN_SCOPES: ReadonlyArray<string> = [
|
2020-05-24 04:38:24 +05:30
|
|
|
'minecraft_server_session',
|
|
|
|
'offline_access',
|
|
|
|
'account_info',
|
|
|
|
'account_email',
|
2017-10-17 23:20:34 +05:30
|
|
|
];
|
2024-12-11 01:12:06 +05:30
|
|
|
|
|
|
|
export function oAuthValidate(oauthData: Pick<OAuthState, 'params' | 'description' | 'prompt' | 'loginHint'>) {
|
2020-05-24 04:38:24 +05:30
|
|
|
// TODO: move to oAuth actions?
|
2024-12-11 01:12:06 +05:30
|
|
|
// auth code flow: /oauth?client_id=ely&redirect_uri=http%3A%2F%2Fely.by&response_type=code&scope=minecraft_server_session&description=foo
|
|
|
|
// device code flow: /code?user_code=XOXOXOXO
|
2020-05-24 04:38:24 +05:30
|
|
|
return wrapInLoader((dispatch) =>
|
|
|
|
oauth
|
2024-12-11 01:12:06 +05:30
|
|
|
.validate(getOAuthRequest(oauthData))
|
2020-05-24 04:38:24 +05:30
|
|
|
.then((resp) => {
|
|
|
|
const { scopes } = resp.session;
|
|
|
|
const invalidScopes = scopes.filter((scope) => !KNOWN_SCOPES.includes(scope));
|
|
|
|
|
|
|
|
if (invalidScopes.length) {
|
|
|
|
logger.error('Got invalid scopes after oauth validation', {
|
|
|
|
invalidScopes,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-11 01:12:06 +05:30
|
|
|
let { prompt } = oauthData;
|
|
|
|
|
|
|
|
if (prompt && !Array.isArray(prompt)) {
|
|
|
|
prompt = prompt.split(',').map((item) => item.trim());
|
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch(setClient(resp.client));
|
|
|
|
dispatch(
|
|
|
|
setOAuthRequest({
|
2024-12-11 01:12:06 +05:30
|
|
|
params: oauthData.params,
|
|
|
|
description: oauthData.description,
|
2020-05-24 04:38:24 +05:30
|
|
|
loginHint: oauthData.loginHint,
|
2024-12-11 01:12:06 +05:30
|
|
|
prompt,
|
2020-05-24 04:38:24 +05:30
|
|
|
}),
|
|
|
|
);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export function oAuthComplete(params: { accept?: boolean } = {}) {
|
2020-05-24 04:38:24 +05:30
|
|
|
return wrapInLoader(
|
|
|
|
async (
|
|
|
|
dispatch,
|
|
|
|
getState,
|
|
|
|
): Promise<{
|
|
|
|
success: boolean;
|
2024-12-11 01:12:06 +05:30
|
|
|
redirectUri?: string;
|
2020-05-24 04:38:24 +05:30
|
|
|
}> => {
|
|
|
|
const oauthData = getState().auth.oauth;
|
|
|
|
|
|
|
|
if (!oauthData) {
|
|
|
|
throw new Error('Can not complete oAuth. Oauth data does not exist');
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
try {
|
2024-12-11 01:12:06 +05:30
|
|
|
const resp = await oauth.complete(getOAuthRequest(oauthData), params);
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
localStorage.removeItem('oauthData');
|
|
|
|
|
2024-12-11 01:12:06 +05:30
|
|
|
if (!resp.redirectUri) {
|
|
|
|
dispatch(setOAuthCode({ success: resp.success && params.accept }));
|
|
|
|
} else if (resp.redirectUri.startsWith('static_page')) {
|
|
|
|
const displayCode = resp.redirectUri.includes('static_page_with_code');
|
2020-05-24 04:38:24 +05:30
|
|
|
|
|
|
|
const [, code] = resp.redirectUri.match(/code=(.+)&/) || [];
|
|
|
|
[, resp.redirectUri] = resp.redirectUri.match(/^(.+)\?/) || [];
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
setOAuthCode({
|
|
|
|
success: resp.success,
|
|
|
|
code,
|
|
|
|
displayCode,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
} catch (error) {
|
|
|
|
const resp:
|
|
|
|
| {
|
|
|
|
acceptRequired: boolean;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
unauthorized: boolean;
|
|
|
|
} = error;
|
|
|
|
|
|
|
|
if ('acceptRequired' in resp) {
|
|
|
|
dispatch(requirePermissionsAccept());
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleOauthParamsValidation(resp);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2016-02-27 16:23:58 +05:30
|
|
|
}
|
|
|
|
|
2024-12-11 01:12:06 +05:30
|
|
|
function getOAuthRequest({ params, description }: OAuthState): OauthRequestData {
|
|
|
|
let data: OauthRequestData;
|
|
|
|
|
|
|
|
if ('userCode' in params) {
|
|
|
|
data = {
|
|
|
|
user_code: params.userCode,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
data = {
|
|
|
|
client_id: params.clientId,
|
|
|
|
redirect_uri: params.redirectUrl,
|
|
|
|
response_type: params.responseType,
|
|
|
|
scope: params.scope,
|
|
|
|
state: params.state,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (description) {
|
|
|
|
data.description = description;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
function handleOauthParamsValidation(
|
2020-05-24 04:38:24 +05:30
|
|
|
resp: {
|
|
|
|
[key: string]: any;
|
|
|
|
userMessage?: string;
|
|
|
|
} = {},
|
2019-11-27 14:33:32 +05:30
|
|
|
) {
|
2024-12-11 01:12:06 +05:30
|
|
|
// TODO: it would be better to dispatch BSOD from the initial request performers
|
|
|
|
if (resp.error !== 'invalid_user_code') {
|
|
|
|
dispatchBsod();
|
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
localStorage.removeItem('oauthData');
|
2016-08-07 20:20:00 +05:30
|
|
|
|
2020-05-24 04:38:24 +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
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.reject(resp);
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface SetClientAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'set_client';
|
|
|
|
payload: Client;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function setClient(payload: Client): SetClientAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: 'set_client',
|
|
|
|
payload,
|
|
|
|
};
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export type ClientAction = SetClientAction;
|
|
|
|
|
|
|
|
interface SetOauthAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'set_oauth';
|
2024-12-11 01:12:06 +05:30
|
|
|
payload: OAuthState | null;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Input data is coming right from the query string, so the names
|
|
|
|
// are the same, as used for initializing OAuth2 request
|
2024-12-11 01:12:06 +05:30
|
|
|
// TODO: filter out allowed properties
|
|
|
|
export function setOAuthRequest(payload: OAuthState | null): SetOauthAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: 'set_oauth',
|
2024-12-11 01:12:06 +05:30
|
|
|
payload,
|
2020-05-24 04:38:24 +05:30
|
|
|
};
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
interface SetOAuthResultAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'set_oauth_result';
|
|
|
|
payload: Pick<OAuthState, 'success' | 'code' | 'displayCode'>;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
2024-12-11 01:12:06 +05:30
|
|
|
export function setOAuthCode(payload: Pick<OAuthState, 'success' | 'code' | 'displayCode'>): SetOAuthResultAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: 'set_oauth_result',
|
|
|
|
payload,
|
|
|
|
};
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
|
2020-07-22 15:31:12 +05:30
|
|
|
export function resetOAuth(): AppAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch): void => {
|
|
|
|
localStorage.removeItem('oauthData');
|
2024-12-11 01:12:06 +05:30
|
|
|
dispatch(setOAuthRequest(null));
|
2020-05-24 04:38:24 +05:30
|
|
|
};
|
2016-11-19 16:24:24 +05:30
|
|
|
}
|
|
|
|
|
2017-02-01 11:55:15 +05:30
|
|
|
/**
|
|
|
|
* Resets all temporary state related to auth
|
|
|
|
*/
|
2020-07-22 15:31:12 +05:30
|
|
|
export function resetAuth(): AppAction {
|
2021-03-28 20:37:46 +05:30
|
|
|
return (dispatch, getState): Promise<void> => {
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch(setLogin(null));
|
|
|
|
dispatch(resetOAuth());
|
|
|
|
// ensure current account is valid
|
2021-03-28 20:37:46 +05:30
|
|
|
const activeAccount = getActiveAccount(getState());
|
2020-05-24 04:38:24 +05:30
|
|
|
|
|
|
|
if (activeAccount) {
|
|
|
|
return Promise.resolve(dispatch(authenticate(activeAccount)))
|
|
|
|
.then(() => {})
|
|
|
|
.catch(() => {
|
|
|
|
// its okay. user will be redirected to an appropriate place
|
|
|
|
});
|
|
|
|
}
|
2018-03-01 03:28:45 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.resolve();
|
|
|
|
};
|
2017-02-01 11:55:15 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface RequestPermissionsAcceptAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'require_permissions_accept';
|
2016-02-27 16:23:58 +05:30
|
|
|
}
|
2016-02-29 23:46:33 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export function requirePermissionsAccept(): RequestPermissionsAcceptAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: 'require_permissions_accept',
|
|
|
|
};
|
2016-03-15 12:06:13 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
export type OAuthAction = SetOauthAction | SetOAuthResultAction | RequestPermissionsAcceptAction;
|
2020-01-18 02:07:52 +05:30
|
|
|
|
|
|
|
interface SetScopesAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'set_scopes';
|
|
|
|
payload: Array<Scope>;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function setScopes(payload: Array<Scope>): SetScopesAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: 'set_scopes',
|
|
|
|
payload,
|
|
|
|
};
|
2016-04-15 01:24:35 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export type ScopesAction = SetScopesAction;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface SetLoadingAction extends ReduxAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
type: 'set_loading_state';
|
|
|
|
payload: boolean;
|
2016-02-29 23:46:33 +05:30
|
|
|
}
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export function setLoadingState(isLoading: boolean): SetLoadingAction {
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
type: 'set_loading_state',
|
|
|
|
payload: isLoading,
|
|
|
|
};
|
2016-04-02 16:28:54 +05:30
|
|
|
}
|
|
|
|
|
2020-07-22 15:31:12 +05:30
|
|
|
function wrapInLoader<T>(fn: AppAction<Promise<T>>): AppAction<Promise<T>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch(setLoadingState(true));
|
|
|
|
const endLoading = () => dispatch(setLoadingState(false));
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return fn(dispatch, getState, undefined).then(
|
|
|
|
(resp) => {
|
|
|
|
endLoading();
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return resp;
|
|
|
|
},
|
|
|
|
(resp) => {
|
|
|
|
endLoading();
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.reject(resp);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
2016-04-02 16:28:54 +05:30
|
|
|
}
|
2016-04-15 09:39:04 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export type LoadingAction = SetLoadingAction;
|
|
|
|
|
2021-03-28 20:37:46 +05:30
|
|
|
function needActivation({ login }: { login: string }) {
|
|
|
|
return setCredentials({
|
|
|
|
login,
|
|
|
|
isActivationRequired: true,
|
2020-05-24 04:38:24 +05:30
|
|
|
});
|
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) {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (oAuthResp: OAuthResponse): Promise<Account> =>
|
|
|
|
dispatch(
|
|
|
|
authenticate({
|
|
|
|
token: oAuthResp.access_token,
|
|
|
|
refreshToken: oAuthResp.refresh_token || null,
|
|
|
|
}),
|
|
|
|
).then((resp) => {
|
|
|
|
dispatch(setLogin(null));
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
});
|
2016-06-04 19:16:39 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
function validationErrorsHandler(
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch: Dispatch,
|
|
|
|
repeatUrl?: string,
|
2020-01-18 02:07:52 +05:30
|
|
|
): (
|
2020-05-24 04:38:24 +05:30
|
|
|
resp: Resp<{
|
|
|
|
errors?: Record<string, string | ValidationError>;
|
|
|
|
data?: Record<string, any>;
|
|
|
|
}>,
|
2020-01-18 02:07:52 +05:30
|
|
|
) => Promise<never> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (resp) => {
|
|
|
|
if (resp.errors) {
|
|
|
|
const [firstError] = Object.keys(resp.errors);
|
|
|
|
const firstErrorObj: ValidationError = {
|
|
|
|
type: resp.errors[firstError] as string,
|
|
|
|
payload: {
|
|
|
|
isGuest: true,
|
|
|
|
repeatUrl: '',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (resp.data) {
|
|
|
|
// TODO: this should be formatted on backend
|
|
|
|
Object.assign(firstErrorObj.payload, resp.data);
|
|
|
|
}
|
2016-05-15 02:23:58 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (['error.key_not_exists', 'error.key_expire'].includes(firstErrorObj.type) && repeatUrl) {
|
|
|
|
// TODO: this should be formatted on backend
|
|
|
|
firstErrorObj.payload.repeatUrl = repeatUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: can I clone the object or its necessary to catch modified errors list on corresponding catches?
|
|
|
|
const { errors } = resp;
|
|
|
|
errors[firstError] = firstErrorObj;
|
|
|
|
|
|
|
|
dispatch(setErrors(errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
};
|
2016-05-15 02:23:58 +05:30
|
|
|
}
|
2020-07-22 15:31:12 +05:30
|
|
|
|
|
|
|
export type Action =
|
|
|
|
| ErrorAction
|
|
|
|
| CredentialsAction
|
|
|
|
| AccountSwitcherAction
|
|
|
|
| LoadingAction
|
|
|
|
| ClientAction
|
|
|
|
| OAuthAction
|
|
|
|
| ScopesAction;
|