Skip to content

Commit 3698669

Browse files
committed
Fix RequireLogin onEnter hook
1 parent 2858d4d commit 3698669

File tree

5 files changed

+30
-34
lines changed

5 files changed

+30
-34
lines changed

src/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import io from 'socket.io-client';
1111
import {Provider} from 'react-redux';
1212
import {reduxReactRouter, ReduxRouter} from 'redux-router';
1313

14-
import routes from './routes';
14+
import getRoutes from './routes';
1515

1616
const client = new ApiClient();
1717

@@ -35,7 +35,7 @@ global.socket = initSocket();
3535

3636
const component = (
3737
<Provider store={store} key="provider">
38-
<ReduxRouter routes={routes} />
38+
<ReduxRouter routes={getRoutes(store)} />
3939
</Provider>
4040
);
4141

src/containers/RequireLogin/RequireLogin.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import {Component, PropTypes} from 'react';
2-
import {connect} from 'react-redux';
1+
import {Component} from 'react';
32

4-
@connect(state => ({user: state.auth.user}))
53
export default class RequireLogin extends Component {
6-
static propTypes = {
7-
user: PropTypes.object,
8-
history: PropTypes.object.isRequired
9-
}
10-
11-
componentWillMount() {
12-
const {history, user} = this.props;
13-
if (!user) {
14-
setTimeout(() => {
15-
history.replaceState(null, '/');
16-
});
17-
}
4+
static onEnter(store) {
5+
return (nextState, replaceState) => {
6+
const { auth: { user }} = store.getState();
7+
if (!user) {
8+
// oops, not logged in, so can't be here!
9+
replaceState(null, '/');
10+
}
11+
};
1812
}
1913

2014
render() {

src/redux/create.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createStore as _createStore, applyMiddleware, compose } from 'redux';
22
import createMiddleware from './middleware/clientMiddleware';
33
import transitionMiddleware from './middleware/transitionMiddleware';
44

5-
export default function createStore(reduxReactRouter, routes, createHistory, client, data) {
5+
export default function createStore(reduxReactRouter, getRoutes, createHistory, client, data) {
66
const middleware = [createMiddleware(client)];
77

88
if (__CLIENT__) {
@@ -21,7 +21,7 @@ export default function createStore(reduxReactRouter, routes, createHistory, cli
2121
finalCreateStore = applyMiddleware(...middleware)(_createStore);
2222
}
2323

24-
finalCreateStore = reduxReactRouter({ routes, createHistory })(finalCreateStore);
24+
finalCreateStore = reduxReactRouter({ getRoutes, createHistory })(finalCreateStore);
2525

2626
const reducer = require('./modules/reducer');
2727
const store = finalCreateStore(reducer, data);

src/routes.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ import {
1313
NotFound,
1414
} from 'containers';
1515

16-
export default (
17-
<Route component={App}>
18-
<Route path="/" component={Home}/>
19-
<Route path="/widgets" component={Widgets}/>
20-
<Route path="/about" component={About}/>
21-
<Route path="/login" component={Login}/>
22-
<Route component={RequireLogin}>
23-
<Route path="/chat" component={Chat}/>
24-
<Route path="/loginSuccess" component={LoginSuccess}/>
16+
export default (store) => {
17+
return (
18+
<Route component={App}>
19+
<Route path="/" component={Home}/>
20+
<Route path="/widgets" component={Widgets}/>
21+
<Route path="/about" component={About}/>
22+
<Route path="/login" component={Login}/>
23+
<Route component={RequireLogin} onEnter={RequireLogin.onEnter(store)}>
24+
<Route path="/chat" component={Chat}/>
25+
<Route path="/loginSuccess" component={LoginSuccess}/>
26+
</Route>
27+
<Route path="/survey" component={Survey}/>
28+
<Route path="*" component={NotFound} status={404} />
2529
</Route>
26-
<Route path="/survey" component={Survey}/>
27-
<Route path="*" component={NotFound} status={404} />
28-
</Route>
29-
);
30+
);
31+
};

src/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {ReduxRouter} from 'redux-router';
1818
import {reduxReactRouter, match} from 'redux-router/server';
1919
import {Provider} from 'react-redux';
2020
import qs from 'query-string';
21-
import routes from './routes';
21+
import getRoutes from './routes';
2222
import getStatusFromRoutes from './helpers/getStatusFromRoutes';
2323

2424
const pretty = new PrettyError();
@@ -58,7 +58,7 @@ app.use((req, res) => {
5858
webpackIsomorphicTools.refresh();
5959
}
6060
const client = new ApiClient(req);
61-
const store = createStore(reduxReactRouter, routes, null, client);
61+
const store = createStore(reduxReactRouter, getRoutes, null, client);
6262

6363
function hydrateOnClient() {
6464
res.send('<!doctype html>\n' +

0 commit comments

Comments
 (0)