mirror of
https://github.com/ascribe/onion.git
synced 2025-02-14 21:10:27 +01:00
Document RedirectProxyHandler
This commit is contained in:
parent
211e253fb8
commit
d86668e759
@ -11,6 +11,14 @@ import AppConstants from '../../../constants/application_constants';
|
|||||||
|
|
||||||
const { object } = React.PropTypes;
|
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}) {
|
export default function RedirectProxyHandler({to, when}) {
|
||||||
return (Component) => {
|
return (Component) => {
|
||||||
return React.createClass({
|
return React.createClass({
|
||||||
@ -41,19 +49,32 @@ export default function RedirectProxyHandler({to, when}) {
|
|||||||
const { query } = this.props.location;
|
const { query } = this.props.location;
|
||||||
const { redirectAuthenticated, redirect } = query;
|
const { redirectAuthenticated, redirect } = query;
|
||||||
|
|
||||||
|
// validate when as an enum, that is either 'loggedIn' or 'loggedOut'.
|
||||||
|
// Otherwise throw an error.
|
||||||
if(when === 'loggedIn' || when === 'loggedOut') {
|
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' ?
|
let exprToValidate = when === 'loggedIn' ?
|
||||||
this.state.currentUser && this.state.currentUser.email :
|
this.state.currentUser && this.state.currentUser.email :
|
||||||
this.state.currentUser && !this.state.currentUser.email;
|
this.state.currentUser && !this.state.currentUser.email;
|
||||||
|
|
||||||
|
// and redirect if `true`.
|
||||||
if(exprToValidate) {
|
if(exprToValidate) {
|
||||||
window.setTimeout(() => this.history.pushState(null, to, query));
|
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) {
|
} else if(!exprToValidate && when === 'loggedIn' && redirect) {
|
||||||
|
|
||||||
delete query.redirect;
|
delete query.redirect;
|
||||||
window.setTimeout(() => this.history.pushState(null, '/' + redirect, query));
|
window.setTimeout(() => this.history.pushState(null, '/' + redirect, query));
|
||||||
|
|
||||||
|
// and when he's logged in already
|
||||||
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
|
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
|
||||||
/*
|
/*
|
||||||
* redirectAuthenticated contains an arbirary path
|
* redirectAuthenticated contains an arbirary path
|
||||||
|
Loading…
Reference in New Issue
Block a user