2015-10-06 18:28:15 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { History } from 'react-router';
|
|
|
|
|
|
|
|
import UserStore from '../../../stores/user_store';
|
|
|
|
import UserActions from '../../../actions/user_actions';
|
|
|
|
|
2015-10-09 13:51:21 +02:00
|
|
|
import AppConstants from '../../../constants/application_constants';
|
|
|
|
|
|
|
|
|
|
|
|
const { object } = React.PropTypes;
|
2015-10-12 17:57:23 +02:00
|
|
|
const WHEN_ENUM = ['loggedIn', 'loggedOut'];
|
2015-10-06 18:28:15 +02:00
|
|
|
|
2015-10-09 15:51:03 +02:00
|
|
|
/**
|
2015-10-12 16:51:03 +02:00
|
|
|
* Can be used in combination with `Route` as an intermediate Handler
|
2015-10-09 15:51:03 +02:00
|
|
|
* 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')
|
|
|
|
*/
|
2015-10-12 17:00:53 +02:00
|
|
|
export default function AuthProxyHandler({to, when}) {
|
2015-10-12 17:57:23 +02:00
|
|
|
|
|
|
|
// 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`);
|
|
|
|
}
|
|
|
|
|
2015-10-06 18:28:15 +02:00
|
|
|
return (Component) => {
|
|
|
|
return React.createClass({
|
2015-10-09 13:51:21 +02:00
|
|
|
propTypes: {
|
|
|
|
location: object
|
|
|
|
},
|
|
|
|
|
2015-10-06 18:28:15 +02:00
|
|
|
mixins: [History],
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
return UserStore.getState();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
UserStore.listen(this.onChange);
|
|
|
|
UserActions.fetchCurrentUser();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.redirectConditionally();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
UserStore.unlisten(this.onChange);
|
|
|
|
},
|
|
|
|
|
|
|
|
redirectConditionally() {
|
2015-10-09 13:51:21 +02:00
|
|
|
const { query } = this.props.location;
|
|
|
|
const { redirectAuthenticated, redirect } = query;
|
|
|
|
|
2015-10-12 17:57:23 +02:00
|
|
|
// 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;
|
2015-10-09 13:51:21 +02:00
|
|
|
|
2015-10-12 17:57:23 +02:00
|
|
|
// and redirect if `true`.
|
|
|
|
if(exprToValidate) {
|
|
|
|
window.setTimeout(() => this.history.replaceState(null, to, query));
|
2015-10-09 15:51:03 +02:00
|
|
|
|
|
|
|
// Otherwise there can also be the case that the backend
|
2015-10-12 14:29:31 +02:00
|
|
|
// wants to redirect the user to a specific route when the user is logged out already
|
2015-10-12 17:57:23 +02:00
|
|
|
} else if(!exprToValidate && when === 'loggedIn' && redirect) {
|
|
|
|
|
|
|
|
delete query.redirect;
|
|
|
|
window.setTimeout(() => this.history.replaceState(null, '/' + redirect, query));
|
|
|
|
|
|
|
|
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
|
|
|
|
/*
|
|
|
|
* redirectAuthenticated contains an arbirary 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 call
|
|
|
|
* `baseUrl` + `redirectAuthenticated`, as it gets rid of queries as well.
|
|
|
|
*/
|
|
|
|
window.location = AppConstants.baseUrl + redirectAuthenticated;
|
2015-10-06 18:28:15 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onChange(state) {
|
|
|
|
this.setState(state);
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Component {...this.props}/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2015-10-12 16:51:03 +02:00
|
|
|
}
|