File tree Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -7,11 +7,13 @@ import SignupPage from './components/signup/SignupPage';
7
7
import LoginPage from './components/login/LoginPage' ;
8
8
import NewEventPage from './components/events/NewEventPage' ;
9
9
10
+ import requireAuth from './utils/requireAuth' ;
11
+
10
12
export default (
11
13
< Route path = "/" component = { App } >
12
14
< IndexRoute component = { Greetings } />
13
15
< Route path = "signup" component = { SignupPage } />
14
16
< Route path = "login" component = { LoginPage } />
15
- < Route path = "new-event" component = { NewEventPage } />
17
+ < Route path = "new-event" component = { requireAuth ( NewEventPage ) } />
16
18
</ Route >
17
19
)
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+ import { connect } from 'react-redux' ;
3
+ import { addFlashMessage } from '../actions/flashMessages' ;
4
+
5
+ export default function ( ComposedComponent ) {
6
+ class Authenticate extends React . Component {
7
+ componentWillMount ( ) {
8
+ if ( ! this . props . isAuthenticated ) {
9
+ this . props . addFlashMessage ( {
10
+ type : 'error' ,
11
+ text : 'You need to login to access this page'
12
+ } ) ;
13
+ this . context . router . push ( '/login' ) ;
14
+ }
15
+ }
16
+
17
+ componentWillUpdate ( nextProps ) {
18
+ if ( ! nextProps . isAuthenticated ) {
19
+ this . context . router . push ( '/' ) ;
20
+ }
21
+ }
22
+
23
+ render ( ) {
24
+ return (
25
+ < ComposedComponent { ...this . props } />
26
+ ) ;
27
+ }
28
+ }
29
+
30
+ Authenticate . propTypes = {
31
+ isAuthenticated : React . PropTypes . bool . isRequired ,
32
+ addFlashMessage : React . PropTypes . func . isRequired
33
+ }
34
+
35
+ Authenticate . contextTypes = {
36
+ router : React . PropTypes . object . isRequired
37
+ }
38
+
39
+ function mapStateToProps ( state ) {
40
+ return {
41
+ isAuthenticated : state . auth . isAuthenticated
42
+ } ;
43
+ }
44
+
45
+ return connect ( mapStateToProps , { addFlashMessage } ) ( Authenticate ) ;
46
+ }
You can’t perform that action at this time.
0 commit comments