1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00

Generalize ProxyHandler to take arbitrary redirect functions

This commit is contained in:
Tim Daubenschütz 2015-12-08 11:17:04 +01:00
parent 7cb5fa6758
commit 50377ba478
4 changed files with 121 additions and 119 deletions

View File

@ -1,7 +1,8 @@
'use strict';
import React from 'react';
import { History, RouteContext } from 'react-router';
import { RouteContext } from 'react-router';
import history from '../../history';
import UserStore from '../../stores/user_store';
import UserActions from '../../actions/user_actions';
@ -12,18 +13,12 @@ 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
* between the actual component we want to display dependent on a certain state
* that is required to display that component.
* Redirects the user conditionally according to his authentication
*
* @param {string} options.to Any type of route path that is defined in routes.js
* @param {enum/string} options.when ('loggedIn' || 'loggedOut')
*/
export function ProxyHandler({to, when}) {
export function AuthRedirect({to, when}) {
// validate `when`, must be contained in `WHEN_ENUM`.
// Throw an error otherwise.
if(WHEN_ENUM.indexOf(when) === -1) {
@ -31,6 +26,52 @@ export function ProxyHandler({to, when}) {
throw new Error(`"when" must be one of: [${whenValues}] got "${when}" instead`);
}
return function(currentUser, query) {
const { redirectAuthenticated, redirect } = query;
// 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' ? currentUser && currentUser.email
: currentUser && !currentUser.email;
// and redirect if `true`.
if(exprToValidate) {
window.setTimeout(() => 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(() => history.replaceState(null, '/' + redirect, query));
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
/*
* redirectAuthenticated contains an arbitrary 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;
}
};
}
/**
* Can be used in combination with `Route` 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 {[function]} redirectFn A function that conditionally redirects
*/
export function ProxyHandler(redirectFn) {
return (Component) => {
return React.createClass({
displayName: 'ProxyHandler',
@ -41,7 +82,7 @@ export function ProxyHandler({to, when}) {
// We need insert `RouteContext` here in order to be able
// to use the `Lifecycle` widget in further down nested components
mixins: [History, RouteContext],
mixins: [RouteContext],
getInitialState() {
return UserStore.getState();
@ -53,10 +94,10 @@ export function ProxyHandler({to, when}) {
},
componentDidUpdate() {
// Only refresh this component, when UserSources are not loading
// data from the server
if(!UserStore.isLoading()) {
this.redirectConditionally();
const { currentUser } = this.state;
const { query } = this.props.location;
redirectFn(currentUser, query);
}
},
@ -64,45 +105,6 @@ export function ProxyHandler({to, when}) {
UserStore.unlisten(this.onChange);
},
redirectConditionally() {
const { query } = this.props.location;
const { redirectAuthenticated, redirect } = query;
// 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.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.replaceState(null, '/' + redirect, query));
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
/*
* redirectAuthenticated contains an arbitrary 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;
}
},
onChange(state) {
this.setState(state);
},

View File

@ -22,7 +22,7 @@ import PasswordResetContainer from '../../password_reset_container';
import CoaVerifyContainer from '../../coa_verify_container';
import ErrorNotFoundPage from '../../error_not_found_page';
import { ProxyHandler } from '../../../components/ascribe_routes/proxy_handler';
import { ProxyHandler, AuthRedirect } from '../../../components/ascribe_routes/proxy_handler';
const ROUTES = {
@ -31,26 +31,26 @@ const ROUTES = {
<IndexRoute component={SPLanding} />
<Route
path='login'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(SPLoginContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(SPLoginContainer)} />
<Route
path='logout'
component={ProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/', when: 'loggedOut'}))(LogoutContainer)}/>
<Route
path='signup'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(SPSignupContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(SPSignupContainer)} />
<Route
path='password_reset'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(PasswordResetContainer)} />
<Route
path='settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SPSettingsContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SPSettingsContainer)}/>
<Route
path='register_piece'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SPRegisterPiece)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SPRegisterPiece)}
headerTitle='+ NEW WORK'/>
<Route
path='collection'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SPPieceList)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SPPieceList)}
headerTitle='COLLECTION'/>
<Route path='pieces/:pieceId' component={SPPieceContainer} />
@ -64,20 +64,20 @@ const ROUTES = {
<IndexRoute component={PRLanding} />
<Route
path='register_piece'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(PRRegisterPiece)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(PRRegisterPiece)}
headerTitle='+ NEW WORK'/>
<Route
path='login'
component={ProxyHandler({to: '/register_piece', when: 'loggedIn'})(SPLoginContainer)} />
component={ProxyHandler(AuthRedirect({to: '/register_piece', when: 'loggedIn'}))(SPLoginContainer)} />
<Route
path='logout'
component={ProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)} />
component={ProxyHandler(AuthRedirect({to: '/', when: 'loggedOut'}))(LogoutContainer)} />
<Route
path='signup'
component={ProxyHandler({to: '/register_piece', when: 'loggedIn'})(SPSignupContainer)} />
component={ProxyHandler(AuthRedirect({to: '/register_piece', when: 'loggedIn'}))(SPSignupContainer)} />
<Route
path='password_reset'
component={ProxyHandler({to: '/register_piece', when: 'loggedIn'})(PasswordResetContainer)} />
component={ProxyHandler(AuthRedirect({to: '/register_piece', when: 'loggedIn'}))(PasswordResetContainer)} />
<Route path='pieces/:pieceId' component={SPPieceContainer} />
<Route path='*' component={ErrorNotFoundPage} />
</Route>

View File

@ -40,7 +40,7 @@ import LumenusLanding from './components/lumenus/lumenus_landing';
import Vivi23Landing from './components/23vivi/23vivi_landing';
import Vivi23PieceList from './components/23vivi/23vivi_piece_list';
import { ProxyHandler } from '../../../components/ascribe_routes/proxy_handler';
import { ProxyHandler, AuthRedirect } from '../../../components/ascribe_routes/proxy_handler';
import WalletApp from './wallet_app';
@ -51,29 +51,29 @@ let ROUTES = {
<IndexRoute component={CylandLanding} />
<Route
path='login'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(LoginContainer)} />
<Route
path='logout'
component={ProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/', when: 'loggedOut'}))(LogoutContainer)}/>
<Route
path='signup'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(SignupContainer)} />
<Route
path='password_reset'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(PasswordResetContainer)} />
<Route
path='settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SettingsContainer)}/>
<Route
path='contract_settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(ContractSettings)}/>
<Route
path='register_piece'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(CylandRegisterPiece)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(CylandRegisterPiece)}
headerTitle='+ NEW WORK'/>
<Route
path='collection'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(CylandPieceList)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(CylandPieceList)}
headerTitle='COLLECTION'/>
<Route path='editions/:editionId' component={EditionContainer} />
<Route path='verify' component={CoaVerifyContainer} />
@ -85,29 +85,29 @@ let ROUTES = {
<Route path='/' component={WalletApp}>
<Route
path='login'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(LoginContainer)} />
<Route
path='logout'
component={ProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/', when: 'loggedOut'}))(LogoutContainer)}/>
<Route
path='signup'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(SignupContainer)} />
<Route
path='password_reset'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(PasswordResetContainer)} />
<Route
path='settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SettingsContainer)}/>
<Route
path='contract_settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(ContractSettings)}/>
<Route
path='register_piece'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(CCRegisterPiece)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(CCRegisterPiece)}
headerTitle='+ NEW WORK'/>
<Route
path='collection'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(PieceList)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(PieceList)}
headerTitle='COLLECTION'/>
<Route path='pieces/:pieceId' component={PieceContainer} />
<Route path='editions/:editionId' component={EditionContainer} />
@ -120,39 +120,39 @@ let ROUTES = {
<IndexRoute component={IkonotvLanding} />
<Route
path='login'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(LoginContainer)} />
<Route
path='logout'
component={ProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/', when: 'loggedOut'}))(LogoutContainer)}/>
<Route
path='signup'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(SignupContainer)} />
<Route
path='password_reset'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(PasswordResetContainer)} />
<Route
path='settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SettingsContainer)}/>
<Route
path='contract_settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(ContractSettings)}/>
<Route
path='request_loan'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SendContractAgreementForm)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SendContractAgreementForm)}
headerTitle='SEND NEW CONTRACT'
aclName='acl_create_contractagreement'/>
<Route
path='register_piece'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(IkonotvRegisterPiece)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(IkonotvRegisterPiece)}
headerTitle='+ NEW WORK'
aclName='acl_create_piece'/>
<Route
path='collection'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(IkonotvPieceList)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(IkonotvPieceList)}
headerTitle='COLLECTION'/>
<Route
path='contract_notifications'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(IkonotvContractNotifications)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(IkonotvContractNotifications)}/>
<Route path='pieces/:pieceId' component={IkonotvPieceContainer} />
<Route path='editions/:editionId' component={EditionContainer} />
<Route path='verify' component={CoaVerifyContainer} />
@ -161,32 +161,32 @@ let ROUTES = {
),
'lumenus': (
<Route path='/' component={WalletApp}>
<IndexRoute component={ProxyHandler({to: '/collection', when: 'loggedIn'})(LumenusLanding)} />
<IndexRoute component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(LumenusLanding)} />
<Route
path='login'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(LoginContainer)} />
<Route
path='logout'
component={ProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/', when: 'loggedOut'}))(LogoutContainer)}/>
<Route
path='signup'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(SignupContainer)} />
<Route
path='password_reset'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(PasswordResetContainer)} />
<Route
path='settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SettingsContainer)}/>
<Route
path='contract_settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(ContractSettings)}/>
<Route
path='register_piece'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(MarketRegisterPiece)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(MarketRegisterPiece)}
headerTitle='+ NEW WORK'/>
<Route
path='collection'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(MarketPieceList)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(MarketPieceList)}
headerTitle='COLLECTION'/>
<Route path='pieces/:pieceId' component={MarketPieceContainer} />
<Route path='editions/:editionId' component={MarketEditionContainer} />
@ -196,33 +196,33 @@ let ROUTES = {
),
'23vivi': (
<Route path='/' component={WalletApp}>
<IndexRoute component={ProxyHandler({to: '/collection', when: 'loggedIn'})(Vivi23Landing)} />
<IndexRoute component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(Vivi23Landing)} />
<Route
path='login'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(LoginContainer)} />
<Route
path='logout'
component={ProxyHandler({to: '/', when: 'loggedOut'})(LogoutContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/', when: 'loggedOut'}))(LogoutContainer)}/>
<Route
path='signup'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(SignupContainer)} />
<Route
path='password_reset'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(PasswordResetContainer)} />
<Route
path='settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SettingsContainer)}/>
<Route
path='contract_settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(ContractSettings)}/>
<Route
path='register_piece'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(MarketRegisterPiece)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(MarketRegisterPiece)}
headerTitle='+ NEW WORK'
aclName='acl_wallet_submit'/>
<Route
path='collection'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(Vivi23PieceList)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(Vivi23PieceList)}
headerTitle='COLLECTION'/>
<Route path='pieces/:pieceId' component={MarketPieceContainer} />
<Route path='editions/:editionId' component={MarketEditionContainer} />

View File

@ -25,39 +25,39 @@ import ErrorNotFoundPage from './components/error_not_found_page';
import RegisterPiece from './components/register_piece';
import { ProxyHandler } from './components/ascribe_routes/proxy_handler';
import { ProxyHandler, AuthRedirect } from './components/ascribe_routes/proxy_handler';
let COMMON_ROUTES = (
<Route path='/' component={App}>
<Route
path='login'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(LoginContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(LoginContainer)} />
<Route
path='register_piece'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(RegisterPiece)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(RegisterPiece)}
headerTitle='+ NEW WORK'/>
<Route
path='collection'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(PieceList)}
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(PieceList)}
headerTitle='COLLECTION'/>
<Route
path='signup'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(SignupContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(SignupContainer)} />
<Route
path='logout'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(LogoutContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(LogoutContainer)}/>
<Route path='pieces/:pieceId' component={PieceContainer} />
<Route path='editions/:editionId' component={EditionContainer} />
<Route
path='password_reset'
component={ProxyHandler({to: '/collection', when: 'loggedIn'})(PasswordResetContainer)} />
component={ProxyHandler(AuthRedirect({to: '/collection', when: 'loggedIn'}))(PasswordResetContainer)} />
<Route
path='settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(SettingsContainer)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SettingsContainer)}/>
<Route
path='contract_settings'
component={ProxyHandler({to: '/login', when: 'loggedOut'})(ContractSettings)}/>
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(ContractSettings)}/>
<Route path='coa_verify' component={CoaVerifyContainer} />
<Route path='*' component={ErrorNotFoundPage} />
</Route>