File tree 10 files changed +64
-57
lines changed 10 files changed +64
-57
lines changed Original file line number Diff line number Diff line change 1
1
import React , { Component , PropTypes } from 'react' ;
2
2
import { connect } from 'react-redux' ;
3
3
import { createSelector } from 'reselect' ;
4
- import { POST_SIGN_IN_PATH , POST_SIGN_OUT_PATH } from 'src/config' ;
5
4
import { authActions , getAuth } from 'src/core/auth' ;
5
+ import { paths } from '../routes' ;
6
6
7
7
8
8
export class App extends Component {
@@ -27,16 +27,15 @@ export class App extends Component {
27
27
const { auth } = this . props ;
28
28
29
29
if ( auth . authenticated && ! nextProps . auth . authenticated ) {
30
- router . replace ( POST_SIGN_OUT_PATH ) ;
30
+ router . replace ( paths . SIGN_IN ) ;
31
31
}
32
32
else if ( ! auth . authenticated && nextProps . auth . authenticated ) {
33
- router . replace ( POST_SIGN_IN_PATH ) ;
33
+ router . replace ( paths . TASKS ) ;
34
34
}
35
35
}
36
36
37
37
signOut ( ) {
38
38
this . props . signOut ( ) ;
39
- window . location . replace ( '/' ) ;
40
39
}
41
40
42
41
render ( ) {
Original file line number Diff line number Diff line change 1
1
import React , { PropTypes } from 'react' ;
2
2
import { Provider } from 'react-redux' ;
3
- import { Route , Router } from 'react-router' ;
3
+ import { Router } from 'react-router' ;
4
+ import { getRoutes } from './routes' ;
4
5
5
- // config
6
- import { SIGN_IN_PATH , TASKS_PATH } from 'src/config' ;
7
6
8
- // components
9
- import App from './app/app' ;
10
- import SignIn from './sign-in/sign-in' ;
11
- import Tasks from './tasks/tasks' ;
12
-
13
-
14
- export default function Root ( { history, onEnter, store} ) {
7
+ export default function Root ( { history, store} ) {
15
8
return (
16
9
< Provider store = { store } >
17
- < Router history = { history } >
18
- < Route component = { App } onEnter = { onEnter } path = "/" >
19
- < Route component = { SignIn } path = { SIGN_IN_PATH } />
20
- < Route component = { Tasks } path = { TASKS_PATH } />
21
- </ Route >
22
- </ Router >
10
+ < Router history = { history } routes = { getRoutes ( store . getState ) } />
23
11
</ Provider >
24
12
) ;
25
13
}
26
14
27
15
Root . propTypes = {
28
16
history : PropTypes . object . isRequired ,
29
- onEnter : PropTypes . func . isRequired ,
30
17
store : PropTypes . object . isRequired
31
18
} ;
Original file line number Diff line number Diff line change
1
+ import { isAuthenticated } from 'src/core/auth' ;
2
+ import App from './app/app' ;
3
+ import SignIn from './sign-in/sign-in' ;
4
+ import Tasks from './tasks/tasks' ;
5
+
6
+
7
+ export const paths = {
8
+ ROOT : '/' ,
9
+ SIGN_IN : '/sign-in' ,
10
+ TASKS : '/'
11
+ } ;
12
+
13
+
14
+ const requireAuth = getState => {
15
+ return ( nextState , replace ) => {
16
+ if ( ! isAuthenticated ( getState ( ) ) ) {
17
+ replace ( paths . SIGN_IN ) ;
18
+ }
19
+ } ;
20
+ } ;
21
+
22
+ const requireUnauth = getState => {
23
+ return ( nextState , replace ) => {
24
+ if ( isAuthenticated ( getState ( ) ) ) {
25
+ replace ( paths . TASKS ) ;
26
+ }
27
+ } ;
28
+ } ;
29
+
30
+
31
+ export const getRoutes = getState => {
32
+ return {
33
+ path : paths . ROOT ,
34
+ component : App ,
35
+ childRoutes : [
36
+ {
37
+ indexRoute : {
38
+ component : Tasks ,
39
+ onEnter : requireAuth ( getState )
40
+ }
41
+ } ,
42
+ {
43
+ path : paths . SIGN_IN ,
44
+ component : SignIn ,
45
+ onEnter : requireUnauth ( getState )
46
+ }
47
+ ]
48
+ } ;
49
+ } ;
Original file line number Diff line number Diff line change @@ -6,9 +6,9 @@ import { Link } from 'react-router';
6
6
export function TaskFilters ( { filter} ) {
7
7
return (
8
8
< ul className = "task-filters" >
9
- < li > < Link className = { classNames ( { active : ! filter } ) } to = "/tasks " > View All</ Link > </ li >
10
- < li > < Link activeClassName = "active" to = { { pathname : '/tasks ' , query : { filter : 'active' } } } > Active</ Link > </ li >
11
- < li > < Link activeClassName = "active" to = { { pathname : '/tasks ' , query : { filter : 'completed' } } } > Completed</ Link > </ li >
9
+ < li > < Link className = { classNames ( { active : ! filter } ) } to = "/" > View All</ Link > </ li >
10
+ < li > < Link activeClassName = "active" to = { { pathname : '/' , query : { filter : 'active' } } } > Active</ Link > </ li >
11
+ < li > < Link activeClassName = "active" to = { { pathname : '/' , query : { filter : 'completed' } } } > Completed</ Link > </ li >
12
12
</ ul >
13
13
) ;
14
14
}
Original file line number Diff line number Diff line change @@ -4,10 +4,3 @@ export const FIREBASE_CONFIG = {
4
4
databaseURL : 'https://todo-react-redux.firebaseio.com' ,
5
5
storageBucket : 'firebase-todo-react-redux.appspot.com'
6
6
} ;
7
-
8
-
9
- // Route paths
10
- export const SIGN_IN_PATH = '/sign-in' ;
11
- export const TASKS_PATH = '/tasks' ;
12
- export const POST_SIGN_IN_PATH = TASKS_PATH ;
13
- export const POST_SIGN_OUT_PATH = SIGN_IN_PATH ;
Original file line number Diff line number Diff line change @@ -5,8 +5,7 @@ import * as authActions from './actions';
5
5
export { authActions } ;
6
6
export * from './action-types' ;
7
7
export { authReducer } from './reducer' ;
8
- export * from './route-resolver' ;
9
- export { getAuth } from './selectors' ;
8
+ export { getAuth , isAuthenticated } from './selectors' ;
10
9
11
10
12
11
export function initAuth ( dispatch ) {
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -2,6 +2,6 @@ export function getAuth(state) {
2
2
return state . auth ;
3
3
}
4
4
5
- export function getAuthenticated ( state ) {
5
+ export function isAuthenticated ( state ) {
6
6
return getAuth ( state ) . authenticated ;
7
7
}
Original file line number Diff line number Diff line change @@ -6,25 +6,20 @@ import { AppContainer } from 'react-hot-loader';
6
6
import { browserHistory } from 'react-router' ;
7
7
import { syncHistoryWithStore } from 'react-router-redux' ;
8
8
9
- import { authRouteResolver , initAuth } from './core/auth' ;
9
+ import { initAuth } from './core/auth' ;
10
10
import configureStore from './core/store' ;
11
11
import Root from './components/root' ;
12
12
13
13
14
14
const store = configureStore ( ) ;
15
15
const syncedHistory = syncHistoryWithStore ( browserHistory , store ) ;
16
- const onEnter = authRouteResolver ( store . getState ) ;
17
16
const rootElement = document . getElementById ( 'root' ) ;
18
17
19
18
20
19
function render ( Root ) {
21
20
ReactDOM . render (
22
21
< AppContainer >
23
- < Root
24
- history = { syncedHistory }
25
- onEnter = { onEnter }
26
- store = { store }
27
- />
22
+ < Root history = { syncedHistory } store = { store } />
28
23
</ AppContainer > ,
29
24
rootElement
30
25
) ;
Original file line number Diff line number Diff line change @@ -15,3 +15,4 @@ import 'redux-thunk';
15
15
import 'classnames' ;
16
16
import 'firebase' ;
17
17
import 'immutable' ;
18
+ import 'reselect' ;
You can’t perform that action at this time.
0 commit comments