1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-26 11:16:28 +02:00

Document RedirectProxyHandler

This commit is contained in:
Tim Daubenschütz 2015-10-09 15:51:03 +02:00
parent 211e253fb8
commit d86668e759

View File

@ -11,6 +11,14 @@ import AppConstants from '../../../constants/application_constants';
const { object } = React.PropTypes;
/**
* Can be used in combination with `ProxyRoute` 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 {string} options.to Any type of route path that is defined in routes.js
* @param {enum/string} options.when ('loggedIn' || 'loggedOut')
*/
export default function RedirectProxyHandler({to, when}) {
return (Component) => {
return React.createClass({
@ -41,19 +49,32 @@ export default function RedirectProxyHandler({to, when}) {
const { query } = this.props.location;
const { redirectAuthenticated, redirect } = query;
// validate when as an enum, that is either 'loggedIn' or 'loggedOut'.
// Otherwise throw an error.
if(when === 'loggedIn' || when === 'loggedOut') {
// 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.
let exprToValidate = when === 'loggedIn' ?
this.state.currentUser && this.state.currentUser.email :
this.state.currentUser && !this.state.currentUser.email;
// and redirect if `true`.
if(exprToValidate) {
window.setTimeout(() => this.history.pushState(null, to, query));
// Otherwise there can also be the case that the backend
// wants to redirect the user to a specific case when he's logged out already
} else if(!exprToValidate && when === 'loggedIn' && redirect) {
delete query.redirect;
window.setTimeout(() => this.history.pushState(null, '/' + redirect, query));
// and when he's logged in already
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
/*
* redirectAuthenticated contains an arbirary path