2015-10-06 18:28:15 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
2016-06-06 16:54:38 +02:00
|
|
|
import withRouter from 'react-router/es6/withRouter';
|
2015-10-06 18:28:15 +02:00
|
|
|
|
2015-12-08 10:23:37 +01:00
|
|
|
import UserStore from '../../stores/user_store';
|
2015-10-06 18:28:15 +02:00
|
|
|
|
2015-12-08 10:23:37 +01:00
|
|
|
import AppConstants from '../../constants/application_constants';
|
2015-10-09 13:51:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
const { object } = React.PropTypes;
|
2015-10-12 17:57:23 +02:00
|
|
|
const WHEN_ENUM = ['loggedIn', 'loggedOut'];
|
2015-10-06 18:28:15 +02:00
|
|
|
|
2015-10-09 15:51:03 +02:00
|
|
|
/**
|
2015-12-08 11:17:04 +01:00
|
|
|
* Redirects the user conditionally according to his authentication
|
2015-10-09 15:51:03 +02:00
|
|
|
*
|
|
|
|
* @param {enum/string} options.when ('loggedIn' || 'loggedOut')
|
|
|
|
*/
|
2016-02-08 10:41:36 +01:00
|
|
|
export function AuthRedirect({ to, when }) {
|
2015-10-12 17:57:23 +02:00
|
|
|
// validate `when`, must be contained in `WHEN_ENUM`.
|
|
|
|
// Throw an error otherwise.
|
2016-01-11 17:52:32 +01:00
|
|
|
if (WHEN_ENUM.indexOf(when) === -1) {
|
|
|
|
const whenValues = WHEN_ENUM.join(', ');
|
2015-10-12 17:57:23 +02:00
|
|
|
throw new Error(`"when" must be one of: [${whenValues}] got "${when}" instead`);
|
|
|
|
}
|
|
|
|
|
2016-06-06 15:40:26 +02:00
|
|
|
return function redirectRoute(router, currentUser, query) {
|
2015-12-08 11:17:04 +01:00
|
|
|
const { redirectAuthenticated, redirect } = query;
|
|
|
|
|
|
|
|
// The user of this handler specifies with `when`, what kind of status
|
|
|
|
// needs to be checked to conditionally do - if that state is `true` -
|
|
|
|
// a redirect.
|
|
|
|
//
|
|
|
|
// So if when === 'loggedIn', we're checking if the user is logged in (and
|
|
|
|
// vice versa)
|
2016-02-05 11:53:33 +01:00
|
|
|
const isLoggedIn = Object.keys(currentUser).length && currentUser.email;
|
2016-01-13 16:03:08 +01:00
|
|
|
const exprToValidate = when === 'loggedIn' ? isLoggedIn : !isLoggedIn;
|
2015-12-08 11:17:04 +01:00
|
|
|
|
|
|
|
// and redirect if `true`.
|
2016-01-11 12:54:15 +01:00
|
|
|
if (exprToValidate) {
|
2016-06-06 15:40:26 +02:00
|
|
|
window.setTimeout(() => router.replace({ query, pathname: to }));
|
2015-12-08 15:21:20 +01:00
|
|
|
return true;
|
2015-12-08 11:17:04 +01:00
|
|
|
|
|
|
|
// Otherwise there can also be the case that the backend
|
|
|
|
// wants to redirect the user to a specific route when the user is logged out already
|
2016-01-11 12:54:15 +01:00
|
|
|
} else if (!exprToValidate && when === 'loggedIn' && redirect) {
|
2015-12-08 11:17:04 +01:00
|
|
|
delete query.redirect;
|
2016-06-06 15:40:26 +02:00
|
|
|
window.setTimeout(() => router.replace({ query, pathname: `/${redirect}` }));
|
2015-12-08 15:21:20 +01:00
|
|
|
return true;
|
2015-12-08 11:17:04 +01:00
|
|
|
|
2016-01-11 12:54:15 +01:00
|
|
|
} else if (!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
|
2015-12-08 11:17:04 +01:00
|
|
|
/*
|
|
|
|
* redirectAuthenticated contains an arbitrary path
|
|
|
|
* eg pieces/<id>, editions/<bitcoin_id>, collection, settings, ...
|
|
|
|
* hence transitionTo cannot be used directly.
|
|
|
|
*
|
|
|
|
* While we're getting rid of `query.redirect` explicitly in the
|
2016-06-02 15:31:21 +02:00
|
|
|
* above `else if` statement, here it's sufficient to just set the
|
|
|
|
* location to `${baseUrl}/${redirectAuthenticated}`, as this will
|
|
|
|
* get rid of queries as well.
|
2015-12-08 11:17:04 +01:00
|
|
|
*/
|
2016-06-02 15:31:21 +02:00
|
|
|
window.location = `${AppConstants.baseUrl}/${redirectAuthenticated}`;
|
2015-12-08 15:21:20 +01:00
|
|
|
return true;
|
2015-12-08 11:17:04 +01:00
|
|
|
}
|
2016-01-11 12:54:15 +01:00
|
|
|
|
2015-12-08 15:21:20 +01:00
|
|
|
return false;
|
2015-12-08 11:17:04 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be used in combination with `Route` as an intermediate Handler
|
|
|
|
* between the actual component we want to display dependent on a certain state
|
|
|
|
* that is required to display that component.
|
|
|
|
*
|
|
|
|
* @param {[function]} redirectFn A function that conditionally redirects
|
|
|
|
*/
|
2015-12-08 15:21:20 +01:00
|
|
|
export function ProxyHandler(...redirectFunctions) {
|
2016-06-06 15:40:26 +02:00
|
|
|
return (Component) => (
|
2016-06-06 16:54:38 +02:00
|
|
|
withRouter(React.createClass({
|
2015-12-08 10:38:32 +01:00
|
|
|
displayName: 'ProxyHandler',
|
2015-11-06 15:10:29 +01:00
|
|
|
|
2015-10-09 13:51:21 +02:00
|
|
|
propTypes: {
|
2016-06-06 16:54:38 +02:00
|
|
|
router: React.PropTypes.object.isRequired,
|
|
|
|
|
2016-02-08 10:41:36 +01:00
|
|
|
// Provided from AscribeApp, after the routes have been initialized
|
|
|
|
currentUser: React.PropTypes.object,
|
2016-01-13 16:03:08 +01:00
|
|
|
whitelabel: React.PropTypes.object,
|
|
|
|
|
|
|
|
// Provided from router
|
2016-01-25 17:53:28 +01:00
|
|
|
location: object,
|
|
|
|
route: object
|
2015-10-09 13:51:21 +02:00
|
|
|
},
|
|
|
|
|
2016-01-25 17:53:28 +01:00
|
|
|
childContextTypes: {
|
2016-06-06 15:40:26 +02:00
|
|
|
route: object
|
2016-01-25 17:53:28 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
route: this.props.route
|
|
|
|
};
|
|
|
|
},
|
2015-10-06 18:28:15 +02:00
|
|
|
|
2016-01-13 16:03:08 +01:00
|
|
|
componentDidMount() {
|
|
|
|
this.evaluateRedirectFunctions();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
this.evaluateRedirectFunctions(nextProps);
|
|
|
|
},
|
|
|
|
|
|
|
|
evaluateRedirectFunctions(props = this.props) {
|
2016-06-06 16:54:38 +02:00
|
|
|
const { currentUser, location: { query }, router } = props;
|
2015-12-08 15:44:46 +01:00
|
|
|
|
2016-01-13 16:03:08 +01:00
|
|
|
if (UserStore.hasLoaded() && !UserStore.isLoading()) {
|
2016-01-11 12:54:15 +01:00
|
|
|
for (let i = 0; i < redirectFunctions.length; i++) {
|
2015-12-08 15:21:20 +01:00
|
|
|
// if a redirectFunction redirects the user,
|
|
|
|
// it should return `true` and therefore
|
|
|
|
// stop/avoid the execution of all functions
|
|
|
|
// that follow
|
2016-06-06 16:54:38 +02:00
|
|
|
if (redirectFunctions[i](router, currentUser, query)) {
|
2015-12-08 15:21:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 11:31:02 +01:00
|
|
|
}
|
2015-10-06 18:28:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2016-01-13 16:03:08 +01:00
|
|
|
<Component {...this.props} />
|
2015-10-06 18:28:15 +02:00
|
|
|
);
|
|
|
|
}
|
2016-06-06 15:40:26 +02:00
|
|
|
})
|
2016-06-06 16:54:38 +02:00
|
|
|
));
|
2015-10-12 16:51:03 +02:00
|
|
|
}
|