1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-29 00:58:03 +02:00

Refactor AuthProxyHandler

This commit is contained in:
vrde 2015-10-12 17:57:23 +02:00
parent 12d3fb289e
commit dc97517d65

View File

@ -10,6 +10,7 @@ import AppConstants from '../../../constants/application_constants';
const { object } = React.PropTypes;
const WHEN_ENUM = ['loggedIn', 'loggedOut'];
/**
* 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')
*/
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 React.createClass({
propTypes: {
@ -49,10 +58,6 @@ export default function AuthProxyHandler({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.
@ -65,14 +70,14 @@ export default function AuthProxyHandler({to, when}) {
// and redirect if `true`.
if(exprToValidate) {
window.setTimeout(() => this.history.pushState(null, to, query));
window.setTimeout(() => this.history.replaceState(null, to, query));
// 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(() => this.history.pushState(null, '/' + redirect, query));
window.setTimeout(() => this.history.replaceState(null, '/' + redirect, query));
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
/*
@ -86,9 +91,6 @@ export default function AuthProxyHandler({to, when}) {
*/
window.location = AppConstants.baseUrl + redirectAuthenticated;
}
} else {
throw new Error('"loggedIn" and "loggedOut" are the only valid inputs for when');
}
},
onChange(state) {