accounts-frontend/src/components/auth/actions.js

400 lines
11 KiB
JavaScript
Raw Normal View History

import { routeActions } from 'react-router-redux';
import logger from 'services/logger';
import history from 'services/history';
import { updateUser, acceptRules as userAcceptRules } from 'components/user/actions';
import { authenticate, logoutAll } from 'components/accounts/actions';
import authentication from 'services/api/authentication';
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';
export { updateUser } from 'components/user/actions';
export { authenticate, logoutAll as logout } from 'components/accounts/actions';
/**
* 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
*/
export function goBack(fallbackUrl = null) {
if (history.canGoBack()) {
return routeActions.goBack();
} else if (fallbackUrl) {
return routeActions.push(fallbackUrl);
}
return {
type: 'noop'
};
}
export function login({login = '', password = '', rememberMe = false}) {
const PASSWORD_REQUIRED = 'error.password_required';
const LOGIN_REQUIRED = 'error.login_required';
const ACTIVATION_REQUIRED = 'error.account_not_activated';
return wrapInLoader((dispatch) =>
authentication.login(
{login, password, 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.login === LOGIN_REQUIRED && password) {
logger.warn('No login on password panel');
return dispatch(logoutAll());
}
}
return validationErrorsHandler(dispatch)(resp);
})
);
}
2016-08-03 00:29:29 +05:30
export function acceptRules() {
return wrapInLoader((dispatch) =>
dispatch(userAcceptRules())
.catch(validationErrorsHandler(dispatch))
);
}
export function forgotPassword({
login = ''
}) {
return wrapInLoader((dispatch, getState) =>
2016-07-28 10:33:30 +05:30
authentication.forgotPassword({login})
.then(({data = {}}) => dispatch(updateUser({
maskedEmail: data.emailMask || getState().user.email
})))
.catch(validationErrorsHandler(dispatch))
);
}
export function recoverPassword({
key = '',
newPassword = '',
newRePassword = ''
}) {
return wrapInLoader((dispatch) =>
2016-07-28 10:33:30 +05:30
authentication.recoverPassword({key, newPassword, newRePassword})
.then(authHandler(dispatch))
.catch(validationErrorsHandler(dispatch, '/forgot-password'))
);
}
export function register({
email = '',
username = '',
password = '',
rePassword = '',
2016-08-05 11:13:46 +05:30
captcha = '',
rulesAgreement = false
}) {
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
})
.then(() => {
dispatch(updateUser({
username,
email
}));
dispatch(needActivation());
dispatch(routeActions.push('/activation'));
})
.catch(validationErrorsHandler(dispatch))
);
}
export function activate({key = ''}) {
return wrapInLoader((dispatch) =>
2016-07-28 10:33:30 +05:30
signup.activate({key})
.then(authHandler(dispatch))
.catch(validationErrorsHandler(dispatch, '/resend-activation'))
);
}
export function resendActivation({email = '', captcha}) {
return wrapInLoader((dispatch) =>
signup.resendActivation({email, captcha})
2016-07-28 10:33:30 +05:30
.then((resp) => {
dispatch(updateUser({
email
}));
2016-07-28 10:33:30 +05:30
return resp;
})
.catch(validationErrorsHandler(dispatch))
);
}
export const SET_LOGIN = 'auth:setLogin';
export function setLogin(login) {
return {
type: SET_LOGIN,
payload: login
};
}
2016-11-13 20:17:56 +05:30
export const SET_SWITCHER = 'auth:setAccountSwitcher';
export function setAccountSwitcher(isOn) {
return {
type: SET_SWITCHER,
payload: isOn
};
}
export const ERROR = 'auth:error';
export function setErrors(errors) {
return {
type: ERROR,
payload: errors,
error: true
};
}
export function clearErrors() {
return setErrors(null);
}
/**
* @param {object} oauthData
* @param {string} oauthData.clientId
* @param {string} oauthData.redirectUrl
* @param {string} oauthData.responseType
* @param {string} oauthData.description
* @param {string} oauthData.scope
* @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
* @param {string} oauthData.state
*
* @return {Promise}
*/
export function oAuthValidate(oauthData) {
// 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) => {
let prompt = (oauthData.prompt || 'none').split(',').map((item) => item.trim);
if (prompt.includes('none')) {
prompt = ['none'];
}
dispatch(setClient(resp.client));
dispatch(setOAuthRequest({
...resp.oAuth,
prompt: oauthData.prompt || 'none',
loginHint: oauthData.loginHint
}));
dispatch(setScopes(resp.session.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
*
* @return {Promise}
*/
2016-02-27 16:23:58 +05:30
export function oAuthComplete(params = {}) {
return wrapInLoader((dispatch, getState) =>
oauth.complete(getState().auth.oauth, params)
.then((resp) => {
2016-08-27 15:49:02 +05:30
localStorage.removeItem('oauthData');
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
}));
}
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);
}
return handleOauthParamsValidation(resp);
})
);
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
return Promise.reject(resp);
2016-02-23 11:27:16 +05:30
}
export const SET_CLIENT = 'set_client';
export function setClient({id, name, description}) {
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() {
return (dispatch) => {
localStorage.removeItem('oauthData');
dispatch(setOAuthRequest({}));
};
}
2016-02-27 16:23:58 +05:30
export const SET_OAUTH = 'set_oauth';
export function setOAuthRequest(oauth) {
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,
2016-02-27 16:23:58 +05:30
state: oauth.state
}
};
}
export const SET_OAUTH_RESULT = 'set_oauth_result';
export function setOAuthCode(oauth) {
return {
type: SET_OAUTH_RESULT,
payload: {
success: oauth.success,
code: oauth.code,
displayCode: oauth.displayCode
}
};
}
export const REQUIRE_PERMISSIONS_ACCEPT = 'require_permissions_accept';
export function requirePermissionsAccept() {
return {
type: REQUIRE_PERMISSIONS_ACCEPT
};
}
export const SET_SCOPES = 'set_scopes';
export function setScopes(scopes) {
if (!(scopes instanceof Array)) {
throw new Error('Scopes must be array');
}
return {
type: SET_SCOPES,
payload: scopes
};
}
export const SET_LOADING_STATE = 'set_loading_state';
export function setLoadingState(isLoading) {
return {
type: SET_LOADING_STATE,
payload: isLoading
};
}
function wrapInLoader(fn) {
return (dispatch, getState) => {
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);
});
};
}
function needActivation() {
return updateUser({
isActive: false,
isGuest: false
});
}
function authHandler(dispatch) {
2016-11-05 15:41:41 +05:30
return (resp) => dispatch(authenticate({
token: resp.access_token,
refreshToken: resp.refresh_token
})).then((resp) => {
dispatch(setLogin(null));
return resp;
});
}
function validationErrorsHandler(dispatch, repeatUrl) {
return (resp) => {
if (resp.errors) {
const firstError = Object.keys(resp.errors)[0];
const error = {
type: resp.errors[firstError],
payload: {
isGuest: true
}
};
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
Object.assign(error.payload, {
repeatUrl
});
}
resp.errors[firstError] = error;
dispatch(setErrors(resp.errors));
}
return Promise.reject(resp);
};
}