mirror of
https://github.com/ascribe/onion.git
synced 2025-01-03 18:35:09 +01:00
Refactor AuthProxyHandler
This commit is contained in:
parent
12d3fb289e
commit
dc97517d65
@ -10,6 +10,7 @@ import AppConstants from '../../../constants/application_constants';
|
|||||||
|
|
||||||
|
|
||||||
const { object } = React.PropTypes;
|
const { object } = React.PropTypes;
|
||||||
|
const WHEN_ENUM = ['loggedIn', 'loggedOut'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Can be used in combination with `Route` as an intermediate Handler
|
* Can be used in combination with `Route` as an intermediate Handler
|
||||||
@ -20,6 +21,14 @@ const { object } = React.PropTypes;
|
|||||||
* @param {enum/string} options.when ('loggedIn' || 'loggedOut')
|
* @param {enum/string} options.when ('loggedIn' || 'loggedOut')
|
||||||
*/
|
*/
|
||||||
export default function AuthProxyHandler({to, when}) {
|
export default function AuthProxyHandler({to, when}) {
|
||||||
|
|
||||||
|
// validate `when`, must be contained in `WHEN_ENUM`.
|
||||||
|
// Throw an error otherwise.
|
||||||
|
if(WHEN_ENUM.indexOf(when) === -1) {
|
||||||
|
let whenValues = WHEN_ENUM.join(', ');
|
||||||
|
throw new Error(`"when" must be one of: [${whenValues}] got "${when}" instead`);
|
||||||
|
}
|
||||||
|
|
||||||
return (Component) => {
|
return (Component) => {
|
||||||
return React.createClass({
|
return React.createClass({
|
||||||
propTypes: {
|
propTypes: {
|
||||||
@ -49,45 +58,38 @@ export default function AuthProxyHandler({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'.
|
// The user of this handler specifies with `when`, what kind of status
|
||||||
// Otherwise throw an error.
|
// needs to be checked to conditionally do - if that state is `true` -
|
||||||
if(when === 'loggedIn' || when === 'loggedOut') {
|
// 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;
|
||||||
|
|
||||||
// The user of this handler specifies with `when`, what kind of status
|
// and redirect if `true`.
|
||||||
// needs to be checked to conditionally do - if that state is `true` -
|
if(exprToValidate) {
|
||||||
// a redirect.
|
window.setTimeout(() => this.history.replaceState(null, to, query));
|
||||||
//
|
|
||||||
// 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
|
// 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
|
// wants to redirect the user to a specific route when the user is 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.replaceState(null, '/' + redirect, query));
|
||||||
|
|
||||||
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
|
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
|
||||||
/*
|
/*
|
||||||
* redirectAuthenticated contains an arbirary path
|
* redirectAuthenticated contains an arbirary path
|
||||||
* eg pieces/<id>, editions/<bitcoin_id>, collection, settings, ...
|
* eg pieces/<id>, editions/<bitcoin_id>, collection, settings, ...
|
||||||
* hence transitionTo cannot be used directly.
|
* hence transitionTo cannot be used directly.
|
||||||
*
|
*
|
||||||
* While we're getting rid of `query.redirect` explicitly in the
|
* While we're getting rid of `query.redirect` explicitly in the
|
||||||
* above `else if` statement, here it's sufficient to just call
|
* above `else if` statement, here it's sufficient to just call
|
||||||
* `baseUrl` + `redirectAuthenticated`, as it gets rid of queries as well.
|
* `baseUrl` + `redirectAuthenticated`, as it gets rid of queries as well.
|
||||||
*/
|
*/
|
||||||
window.location = AppConstants.baseUrl + redirectAuthenticated;
|
window.location = AppConstants.baseUrl + redirectAuthenticated;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new Error('"loggedIn" and "loggedOut" are the only valid inputs for when');
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user