mirror of
https://github.com/ascribe/onion.git
synced 2025-01-03 10:25:08 +01:00
Implement AuthPrizeRoleRedirect
This commit is contained in:
parent
50377ba478
commit
e427429263
@ -41,6 +41,7 @@ export function AuthRedirect({to, when}) {
|
||||
// and redirect if `true`.
|
||||
if(exprToValidate) {
|
||||
window.setTimeout(() => history.replaceState(null, to, query));
|
||||
return true;
|
||||
|
||||
// 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
|
||||
@ -48,6 +49,7 @@ export function AuthRedirect({to, when}) {
|
||||
|
||||
delete query.redirect;
|
||||
window.setTimeout(() => history.replaceState(null, '/' + redirect, query));
|
||||
return true;
|
||||
|
||||
} else if(!exprToValidate && when === 'loggedOut' && redirectAuthenticated) {
|
||||
/*
|
||||
@ -60,7 +62,9 @@ export function AuthRedirect({to, when}) {
|
||||
* `baseUrl` + `redirectAuthenticated`, as it gets rid of queries as well.
|
||||
*/
|
||||
window.location = AppConstants.baseUrl + redirectAuthenticated;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
@ -71,7 +75,7 @@ export function AuthRedirect({to, when}) {
|
||||
*
|
||||
* @param {[function]} redirectFn A function that conditionally redirects
|
||||
*/
|
||||
export function ProxyHandler(redirectFn) {
|
||||
export function ProxyHandler(...redirectFunctions) {
|
||||
return (Component) => {
|
||||
return React.createClass({
|
||||
displayName: 'ProxyHandler',
|
||||
@ -97,7 +101,15 @@ export function ProxyHandler(redirectFn) {
|
||||
if(!UserStore.isLoading()) {
|
||||
const { currentUser } = this.state;
|
||||
const { query } = this.props.location;
|
||||
redirectFn(currentUser, query);
|
||||
for(let i = 0; i < redirectFunctions.length; i++) {
|
||||
// if a redirectFunction redirects the user,
|
||||
// it should return `true` and therefore
|
||||
// stop/avoid the execution of all functions
|
||||
// that follow
|
||||
if(redirectFunctions[i](currentUser, query)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
import history from '../../../../../../history';
|
||||
|
||||
|
||||
export function AuthPrizeRoleRedirect({ to, when }) {
|
||||
if(when.constructor !== Array && when.length) {
|
||||
throw new Error('`when` of AuthPrizeRoleRedirect must be an array containing values');
|
||||
}
|
||||
if(!to && to.indexOf('/') !== -1) {
|
||||
throw new Error('`to` of AuthPrizeRoleRedirect must be defined and contain a valid route');
|
||||
}
|
||||
|
||||
return function(currentUser, query) {
|
||||
const exprToValidate = when
|
||||
.map((role) => currentUser[role])
|
||||
.reduce((a, b) => a || b);
|
||||
|
||||
if(exprToValidate) {
|
||||
window.setTimeout(() => history.replaceState(null, to, query));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
@ -4,6 +4,7 @@ import React from 'react';
|
||||
import GlobalNotification from '../../../global_notification';
|
||||
|
||||
import Hero from './components/pr_hero';
|
||||
import Header from '../../../header';
|
||||
|
||||
import UserStore from '../../../../stores/user_store';
|
||||
import UserActions from '../../../../actions/user_actions';
|
||||
@ -42,17 +43,26 @@ let PRApp = React.createClass({
|
||||
render() {
|
||||
const { history, children } = this.props;
|
||||
const { currentUser } = this.state;
|
||||
let style = {};
|
||||
let subdomain = getSubdomain();
|
||||
let header;
|
||||
|
||||
|
||||
if (currentUser && currentUser.email && history.isActive(`/pieces/${getCookie(currentUser.email)}`)) {
|
||||
header = <Hero />;
|
||||
style = { paddingTop: '0 !important' };
|
||||
} else if(history.isActive('/collection') || history.isActive('/settings')) {
|
||||
header = <Header />;
|
||||
} else {
|
||||
style = { paddingTop: '0 !important' };
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
{header}
|
||||
<div className={'container ascribe-prize-app client--' + subdomain}>
|
||||
<div
|
||||
style={style}
|
||||
className={'container ascribe-prize-app client--' + subdomain}>
|
||||
{children}
|
||||
<GlobalNotification />
|
||||
<div id="modal" className="container"></div>
|
||||
|
@ -23,6 +23,7 @@ import CoaVerifyContainer from '../../coa_verify_container';
|
||||
import ErrorNotFoundPage from '../../error_not_found_page';
|
||||
|
||||
import { ProxyHandler, AuthRedirect } from '../../../components/ascribe_routes/proxy_handler';
|
||||
import { AuthPrizeRoleRedirect } from './portfolioreview/components/pr_routes/pr_proxy_handler';
|
||||
|
||||
|
||||
const ROUTES = {
|
||||
@ -52,7 +53,6 @@ const ROUTES = {
|
||||
path='collection'
|
||||
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SPPieceList)}
|
||||
headerTitle='COLLECTION'/>
|
||||
|
||||
<Route path='pieces/:pieceId' component={SPPieceContainer} />
|
||||
<Route path='editions/:editionId' component={EditionContainer} />
|
||||
<Route path='verify' component={CoaVerifyContainer} />
|
||||
@ -66,9 +66,16 @@ const ROUTES = {
|
||||
path='register_piece'
|
||||
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(PRRegisterPiece)}
|
||||
headerTitle='+ NEW WORK'/>
|
||||
<Route
|
||||
path='collection'
|
||||
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SPPieceList)}
|
||||
headerTitle='COLLECTION'/>
|
||||
<Route
|
||||
path='login'
|
||||
component={ProxyHandler(AuthRedirect({to: '/register_piece', when: 'loggedIn'}))(SPLoginContainer)} />
|
||||
component={ProxyHandler(
|
||||
AuthPrizeRoleRedirect({ to: '/collection', when: ['is_admin', 'is_judge', 'is_jury'] }),
|
||||
AuthRedirect({to: '/register_piece', when: 'loggedIn'})
|
||||
)(SPLoginContainer)} />
|
||||
<Route
|
||||
path='logout'
|
||||
component={ProxyHandler(AuthRedirect({to: '/', when: 'loggedOut'}))(LogoutContainer)} />
|
||||
@ -78,7 +85,12 @@ const ROUTES = {
|
||||
<Route
|
||||
path='password_reset'
|
||||
component={ProxyHandler(AuthRedirect({to: '/register_piece', when: 'loggedIn'}))(PasswordResetContainer)} />
|
||||
<Route
|
||||
path='settings'
|
||||
component={ProxyHandler(AuthRedirect({to: '/login', when: 'loggedOut'}))(SPSettingsContainer)}/>
|
||||
<Route path='pieces/:pieceId' component={SPPieceContainer} />
|
||||
<Route path='editions/:editionId' component={EditionContainer} />
|
||||
<Route path='verify' component={CoaVerifyContainer} />
|
||||
<Route path='*' component={ErrorNotFoundPage} />
|
||||
</Route>
|
||||
)
|
||||
|
@ -2,7 +2,6 @@ $pr--nav-fg-prim-color: black;
|
||||
$pr--button-color: $pr--nav-fg-prim-color;
|
||||
|
||||
.client--portfolioreview {
|
||||
padding-top: 0 !important;
|
||||
|
||||
.btn-wide,
|
||||
.btn-default {
|
||||
|
Loading…
Reference in New Issue
Block a user