1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-14 09:05:08 +01:00

Refactor redirect and redirectAuthenticated

This commit is contained in:
Tim Daubenschütz 2015-10-09 13:51:21 +02:00
parent 5e2240dbd3
commit 299007baab
5 changed files with 49 additions and 62 deletions

View File

@ -44,10 +44,6 @@ let LoginForm = React.createClass({
componentDidMount() {
UserStore.listen(this.onChange);
let { redirect } = this.props.location.query;
if (redirect && redirect !== 'login'){
this.histoy.pushState(null, '/' + redirect, this.props.location.query);
}
},
componentWillUnmount() {
@ -62,25 +58,8 @@ let LoginForm = React.createClass({
let notification = new GlobalNotificationModel('Login successful', 'success', 10000);
GlobalNotificationActions.appendGlobalNotification(notification);
if(success && this.props.redirectOnLoggedIn && this.props.redirectOnLoginSuccess) {
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;
} 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';
}
if(success) {
UserActions.fetchCurrentUser();
}
},

View File

@ -15,7 +15,6 @@ import Form from './form';
import Property from './property';
import InputCheckbox from './input_checkbox';
import AppConstants from '../../constants/application_constants';
import ApiUrls from '../../constants/api_urls';
@ -43,28 +42,6 @@ let SignupForm = React.createClass({
componentDidMount() {
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() {
@ -75,7 +52,7 @@ let SignupForm = React.createClass({
this.setState(state);
},
handleSuccess(response){
handleSuccess(response) {
if (response.user) {
let notification = new GlobalNotificationModel(getLangText('Sign up successful'), 'success', 50000);
GlobalNotificationActions.appendGlobalNotification(notification);

View File

@ -6,10 +6,18 @@ import { History } from 'react-router';
import UserStore from '../../../stores/user_store';
import UserActions from '../../../actions/user_actions';
import AppConstants from '../../../constants/application_constants';
const { object } = React.PropTypes;
export default function RedirectProxyHandler({to, when}) {
return (Component) => {
return React.createClass({
propTypes: {
location: object
},
mixins: [History],
getInitialState() {
@ -30,13 +38,29 @@ export default function RedirectProxyHandler({to, when}) {
},
redirectConditionally() {
const { query } = this.props.location;
const { redirectAuthenticated, redirect } = query;
if(when === 'loggedIn' || when === 'loggedOut') {
let exprToValidate = when === 'loggedIn' ?
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 {
throw new Error('"loggedIn" and "loggedOut" are the only valid inputs for when');

View File

@ -3,6 +3,9 @@
import React from 'react';
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 WhitelabelStore from '../stores/whitelabel_store';
@ -113,14 +116,18 @@ let RegisterPiece = React.createClass( {
render() {
return (
<RegisterPieceForm
{...this.props}
isFineUploaderActive={this.state.isFineUploaderActive}
handleSuccess={this.handleSuccess}
location={this.props.location}>
{this.props.children}
{this.getSpecifyEditions()}
</RegisterPieceForm>
<Row className="no-margin">
<Col xs={12} sm={10} md={8} smOffset={1} mdOffset={2}>
<RegisterPieceForm
{...this.props}
isFineUploaderActive={this.state.isFineUploaderActive}
handleSuccess={this.handleSuccess}
location={this.props.location}>
{this.props.children}
{this.getSpecifyEditions()}
</RegisterPieceForm>
</Col>
</Row>
);
}
});

View File

@ -35,11 +35,6 @@ let baseUrl = AppConstants.baseUrl;
let COMMON_ROUTES = (
<Route path={baseUrl} component={App}>
<ProxyRoute
path="collection"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
component={PieceList}
headerTitle="COLLECTION"/>
<ProxyRoute
path="login"
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})}
@ -49,6 +44,11 @@ let COMMON_ROUTES = (
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
component={RegisterPiece}
headerTitle="+ NEW WORK"/>
<ProxyRoute
path="collection"
proxyHandler={RedirectProxyHandler({to: '/login', when: 'loggedOut'})}
component={PieceList}
headerTitle="COLLECTION"/>
<ProxyRoute
path="signup"
proxyHandler={RedirectProxyHandler({to: '/collection', when: 'loggedIn'})}