1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 18:13:49 +01:00
onion/js/components/ascribe_routes/proxy_handler.js

132 lines
4.9 KiB
JavaScript
Raw Normal View History

'use strict';
import React from 'react';
2015-12-08 10:23:37 +01:00
import UserStore from '../../stores/user_store';
import withContext from '../context/with_context';
2016-06-08 14:54:05 +02:00
import { currentUserShape, locationShape, routerShape, whitelabelShape } from '../prop_types';
2015-12-08 10:23:37 +01:00
import AppConstants from '../../constants/application_constants';
const { bool, object } = React.PropTypes;
2015-10-12 17:57:23 +02:00
const WHEN_ENUM = ['loggedIn', 'loggedOut'];
2015-10-09 15:51:03 +02: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`);
}
return function redirectRoute(router, { query }, { isLoggedIn }) {
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)
const exprToValidate = when === 'loggedIn' ? isLoggedIn : !isLoggedIn;
// and redirect if `true`.
if (exprToValidate) {
window.setTimeout(() => router.replace({ query, pathname: to }));
2015-12-08 15:21:20 +01:00
return true;
// 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
} else if (!exprToValidate && when === 'loggedIn' && redirect) {
delete query.redirect;
window.setTimeout(() => router.replace({ query, pathname: `/${redirect}` }));
2015-12-08 15:21:20 +01:00
return true;
} else if (!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
/*
* 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
* 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.
*/
window.location = `${AppConstants.baseUrl}/${redirectAuthenticated}`;
2015-12-08 15:21:20 +01:00
return true;
}
2015-12-08 15:21:20 +01:00
return false;
};
}
/**
* 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) {
return (Component) => {
// Don't worry about shadowing the HOC here; using a declaration like this allows
// babel-plugin-react-display-name to automatically generate the displayName.
// eslint-disable-next-line no-shadow
const ProxyHandler = React.createClass({
propTypes: {
// Injected through HOCs
currentUser: currentUserShape.isRequired,
isLoggedIn: bool.isRequired,
2016-06-08 14:54:05 +02:00
location: locationShape.isRequired,
router: routerShape.isRequired,
whitelabel: whitelabelShape.isRequired
},
componentDidMount() {
this.evaluateRedirectFunctions();
},
componentWillReceiveProps(nextProps) {
this.evaluateRedirectFunctions(nextProps);
},
evaluateRedirectFunctions(props = this.props) {
const { currentUser, isLoggedIn, location, router, whitelabel } = props;
if (UserStore.hasLoaded() && !UserStore.isLoading()) {
const context = { currentUser, isLoggedIn, whitelabel };
for (let i = 0; i < redirectFunctions.length; i++) {
// if a redirectFunction redirects the user, it should return `true` and
// therefore stop/avoid the execution of all functions that follow
if (redirectFunctions[i](router, location, context)) {
2015-12-08 15:21:20 +01:00
break;
}
}
}
},
render() {
return (
<Component {...this.props} />
);
}
});
return withContext(ProxyHandler,
2016-06-08 14:54:05 +02:00
'currentUser',
'isLoggedIn',
'location',
'router',
'whitelabel');
};
2015-10-12 16:51:03 +02:00
}