mirror of
https://github.com/ascribe/onion.git
synced 2024-11-15 01:25:17 +01:00
Refactor redirect and redirectAuthenticated
This commit is contained in:
parent
5e2240dbd3
commit
299007baab
@ -44,10 +44,6 @@ let LoginForm = React.createClass({
|
|||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
UserStore.listen(this.onChange);
|
UserStore.listen(this.onChange);
|
||||||
let { redirect } = this.props.location.query;
|
|
||||||
if (redirect && redirect !== 'login'){
|
|
||||||
this.histoy.pushState(null, '/' + redirect, this.props.location.query);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
@ -62,25 +58,8 @@ let LoginForm = React.createClass({
|
|||||||
let notification = new GlobalNotificationModel('Login successful', 'success', 10000);
|
let notification = new GlobalNotificationModel('Login successful', 'success', 10000);
|
||||||
GlobalNotificationActions.appendGlobalNotification(notification);
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
||||||
|
|
||||||
if(success && this.props.redirectOnLoggedIn && this.props.redirectOnLoginSuccess) {
|
if(success) {
|
||||||
let { redirectAuthenticated } = this.props.location.query;
|
UserActions.fetchCurrentUser();
|
||||||
if(redirectAuthenticated) {
|
|
||||||
/*
|
|
||||||
* redirectAuthenticated contains an arbirary path
|
|
||||||
* eg pieces/<id>, editions/<bitcoin_id>, collection, settings, ...
|
|
||||||
* hence transitionTo cannot be used directly
|
|
||||||
*/
|
|
||||||
window.location = AppConstants.baseUrl + redirectAuthenticated;
|
|
||||||
} else {
|
|
||||||
/* Taken from http://stackoverflow.com/a/14916411 */
|
|
||||||
/*
|
|
||||||
We actually have to trick the Browser into showing the "save password" dialog
|
|
||||||
as Chrome expects the login page to be reloaded after the login.
|
|
||||||
Users on Stack Overflow claim this is a bug in chrome and should be fixed in the future.
|
|
||||||
Until then, we redirect the HARD way, but reloading the whole page using window.location
|
|
||||||
*/
|
|
||||||
window.location = AppConstants.baseUrl + 'collection';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -15,7 +15,6 @@ import Form from './form';
|
|||||||
import Property from './property';
|
import Property from './property';
|
||||||
import InputCheckbox from './input_checkbox';
|
import InputCheckbox from './input_checkbox';
|
||||||
|
|
||||||
import AppConstants from '../../constants/application_constants';
|
|
||||||
import ApiUrls from '../../constants/api_urls';
|
import ApiUrls from '../../constants/api_urls';
|
||||||
|
|
||||||
|
|
||||||
@ -43,28 +42,6 @@ let SignupForm = React.createClass({
|
|||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
UserStore.listen(this.onChange);
|
UserStore.listen(this.onChange);
|
||||||
|
|
||||||
let { redirect } = this.props.location.query;
|
|
||||||
if (redirect && redirect !== 'signup'){
|
|
||||||
this.history.pushState(null, '/' + redirect, this.props.location.query);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
componentDidUpdate() {
|
|
||||||
// if user is already logged in, redirect him to piece list
|
|
||||||
if(this.state.currentUser && this.state.currentUser.email) {
|
|
||||||
//this.history.pushState(null, '/collection');
|
|
||||||
let { redirectAuthenticated } = this.props.location.query;
|
|
||||||
if(redirectAuthenticated) {
|
|
||||||
/*
|
|
||||||
* redirectAuthenticated contains an arbirary path
|
|
||||||
* eg pieces/<id>, editions/<bitcoin_id>, collection, settings, ...
|
|
||||||
* hence transitionTo cannot be used directly
|
|
||||||
*/
|
|
||||||
window.location = AppConstants.baseUrl + redirectAuthenticated;
|
|
||||||
}
|
|
||||||
this.history.pushState(null, '/collection');
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
@ -75,7 +52,7 @@ let SignupForm = React.createClass({
|
|||||||
this.setState(state);
|
this.setState(state);
|
||||||
},
|
},
|
||||||
|
|
||||||
handleSuccess(response){
|
handleSuccess(response) {
|
||||||
if (response.user) {
|
if (response.user) {
|
||||||
let notification = new GlobalNotificationModel(getLangText('Sign up successful'), 'success', 50000);
|
let notification = new GlobalNotificationModel(getLangText('Sign up successful'), 'success', 50000);
|
||||||
GlobalNotificationActions.appendGlobalNotification(notification);
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
||||||
|
@ -6,10 +6,18 @@ import { History } from 'react-router';
|
|||||||
import UserStore from '../../../stores/user_store';
|
import UserStore from '../../../stores/user_store';
|
||||||
import UserActions from '../../../actions/user_actions';
|
import UserActions from '../../../actions/user_actions';
|
||||||
|
|
||||||
|
import AppConstants from '../../../constants/application_constants';
|
||||||
|
|
||||||
|
|
||||||
|
const { object } = React.PropTypes;
|
||||||
|
|
||||||
export default function RedirectProxyHandler({to, when}) {
|
export default function RedirectProxyHandler({to, when}) {
|
||||||
return (Component) => {
|
return (Component) => {
|
||||||
return React.createClass({
|
return React.createClass({
|
||||||
|
propTypes: {
|
||||||
|
location: object
|
||||||
|
},
|
||||||
|
|
||||||
mixins: [History],
|
mixins: [History],
|
||||||
|
|
||||||
getInitialState() {
|
getInitialState() {
|
||||||
@ -30,13 +38,29 @@ export default function RedirectProxyHandler({to, when}) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
redirectConditionally() {
|
redirectConditionally() {
|
||||||
|
const { query } = this.props.location;
|
||||||
|
const { redirectAuthenticated, redirect } = query;
|
||||||
|
|
||||||
if(when === 'loggedIn' || when === 'loggedOut') {
|
if(when === 'loggedIn' || when === 'loggedOut') {
|
||||||
|
|
||||||
let exprToValidate = when === 'loggedIn' ?
|
let exprToValidate = when === 'loggedIn' ?
|
||||||
this.state.currentUser && this.state.currentUser.email :
|
this.state.currentUser && this.state.currentUser.email :
|
||||||
this.state.currentUser && !this.state.currentUser.email;
|
this.state.currentUser && !this.state.currentUser.email;
|
||||||
if(exprToValidate) {
|
|
||||||
window.setTimeout(() => this.history.pushState(null, to));
|
if(!exprToValidate && when === 'loggedIn' && redirect) {
|
||||||
|
|
||||||
|
delete query.redirect;
|
||||||
|
window.setTimeout(() => this.history.pushState(null, '/' + redirect, query));
|
||||||
|
|
||||||
|
} else if(exprToValidate) {
|
||||||
|
window.setTimeout(() => this.history.pushState(null, to, 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
|
||||||
|
*/
|
||||||
|
window.location = AppConstants.baseUrl + redirectAuthenticated;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error('"loggedIn" and "loggedOut" are the only valid inputs for when');
|
throw new Error('"loggedIn" and "loggedOut" are the only valid inputs for when');
|
||||||
|
@ -3,6 +3,9 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { History } from 'react-router';
|
import { History } from 'react-router';
|
||||||
|
|
||||||
|
import Col from 'react-bootstrap/lib/Col';
|
||||||
|
import Row from 'react-bootstrap/lib/Row';
|
||||||
|
|
||||||
import WhitelabelActions from '../actions/whitelabel_actions';
|
import WhitelabelActions from '../actions/whitelabel_actions';
|
||||||
import WhitelabelStore from '../stores/whitelabel_store';
|
import WhitelabelStore from '../stores/whitelabel_store';
|
||||||
|
|
||||||
@ -113,14 +116,18 @@ let RegisterPiece = React.createClass( {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<RegisterPieceForm
|
<Row className="no-margin">
|
||||||
{...this.props}
|
<Col xs={12} sm={10} md={8} smOffset={1} mdOffset={2}>
|
||||||
isFineUploaderActive={this.state.isFineUploaderActive}
|
<RegisterPieceForm
|
||||||
handleSuccess={this.handleSuccess}
|
{...this.props}
|
||||||
location={this.props.location}>
|
isFineUploaderActive={this.state.isFineUploaderActive}
|
||||||
{this.props.children}
|
handleSuccess={this.handleSuccess}
|
||||||
{this.getSpecifyEditions()}
|
location={this.props.location}>
|
||||||
</RegisterPieceForm>
|
{this.props.children}
|
||||||
|
{this.getSpecifyEditions()}
|
||||||
|
</RegisterPieceForm>
|
||||||
|
</Col>
|
||||||
|
</Row>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
10
js/routes.js
10
js/routes.js
@ -35,11 +35,6 @@ let baseUrl = AppConstants.baseUrl;
|
|||||||
|
|
||||||
let COMMON_ROUTES = (
|
let COMMON_ROUTES = (
|
||||||
<Route path={baseUrl} component={App}>
|
<Route path={baseUrl} component={App}>
|
||||||
<ProxyRoute
|
|
||||||
path="collection"
|
|
||||||
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
|
|
||||||
component={PieceList}
|
|
||||||
headerTitle="COLLECTION"/>
|
|
||||||
<ProxyRoute
|
<ProxyRoute
|
||||||
path="login"
|
path="login"
|
||||||
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})}
|
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})}
|
||||||
@ -49,6 +44,11 @@ let COMMON_ROUTES = (
|
|||||||
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
|
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
|
||||||
component={RegisterPiece}
|
component={RegisterPiece}
|
||||||
headerTitle="+ NEW WORK"/>
|
headerTitle="+ NEW WORK"/>
|
||||||
|
<ProxyRoute
|
||||||
|
path="collection"
|
||||||
|
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
|
||||||
|
component={PieceList}
|
||||||
|
headerTitle="COLLECTION"/>
|
||||||
<ProxyRoute
|
<ProxyRoute
|
||||||
path="signup"
|
path="signup"
|
||||||
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})}
|
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})}
|
||||||
|
Loading…
Reference in New Issue
Block a user