@@ -2,57 +2,64 @@ var React = require('react');
2
2
var Router = require ( 'react-router' ) ;
3
3
var { Route, RouteHandler, Link } = Router ;
4
4
5
- var App = React . createClass ( {
6
- getInitialState : function ( ) {
7
- return {
5
+ class App extends React . Component {
6
+ constructor ( ) {
7
+ this . state = {
8
8
loggedIn : auth . loggedIn ( )
9
9
} ;
10
- } ,
10
+ }
11
11
12
- setStateOnAuth : function ( loggedIn ) {
12
+ setStateOnAuth ( loggedIn ) {
13
13
this . setState ( {
14
14
loggedIn : loggedIn
15
15
} ) ;
16
- } ,
16
+ }
17
17
18
- componentWillMount : function ( ) {
19
- auth . onChange = this . setStateOnAuth ;
18
+ componentWillMount ( ) {
19
+ auth . onChange = this . setStateOnAuth . bind ( this ) ;
20
20
auth . login ( ) ;
21
- } ,
21
+ }
22
22
23
- render : function ( ) {
24
- var loginOrOut = this . state . loggedIn ?
25
- < Link to = "logout" > Log out</ Link > :
26
- < Link to = "login" > Sign in</ Link > ;
23
+ render ( ) {
27
24
return (
28
25
< div >
29
26
< ul >
30
- < li > { loginOrOut } </ li >
27
+ < li >
28
+ { this . state . loggedIn ? (
29
+ < Link to = "logout" > Log out</ Link >
30
+ ) : (
31
+ < Link to = "login" > Sign in</ Link >
32
+ ) }
33
+ </ li >
31
34
< li > < Link to = "about" > About</ Link > </ li >
32
35
< li > < Link to = "dashboard" > Dashboard</ Link > (authenticated)</ li >
33
36
</ ul >
34
37
< RouteHandler />
35
38
</ div >
36
39
) ;
37
40
}
38
- } ) ;
41
+ }
39
42
40
- var Authentication = {
41
- statics : {
42
- willTransitionTo : function ( transition ) {
43
- var nextPath = transition . path ;
44
- if ( ! auth . loggedIn ( ) ) {
45
- transition . redirect ( '/login' , { } ,
46
- { 'nextPath' : nextPath } ) ;
47
- }
43
+ var requireAuth = ( Component ) => {
44
+ class Authenticated extends React . Component {
45
+ render ( ) {
46
+ return < Component { ...this . props } />
48
47
}
49
48
}
50
- } ;
51
49
52
- var Dashboard = React . createClass ( {
53
- mixins : [ Authentication ] ,
50
+ Authenticated . willTransitionTo = function ( transition ) {
51
+ var nextPath = transition . path ;
52
+ if ( ! auth . loggedIn ( ) ) {
53
+ transition . redirect ( '/login' , { } ,
54
+ { 'nextPath' : nextPath } ) ;
55
+ }
56
+ } ;
57
+
58
+ return Authenticated ;
59
+ } ;
54
60
55
- render : function ( ) {
61
+ var Dashboard = requireAuth ( class extends React . Component {
62
+ render ( ) {
56
63
var token = auth . getToken ( ) ;
57
64
return (
58
65
< div >
@@ -64,77 +71,78 @@ var Dashboard = React.createClass({
64
71
}
65
72
} ) ;
66
73
67
- var Login = React . createClass ( {
68
-
69
- contextTypes : {
70
- router : React . PropTypes . func
71
- } ,
74
+ class Login extends React . Component {
72
75
73
- getInitialState : function ( ) {
74
- return {
76
+ constructor ( ) {
77
+ this . handleSubmit = this . handleSubmit . bind ( this ) ;
78
+ this . state = {
75
79
error : false
76
80
} ;
77
- } ,
81
+ }
78
82
79
- handleSubmit : function ( event ) {
83
+ handleSubmit ( event ) {
80
84
event . preventDefault ( ) ;
81
85
var { router } = this . context ;
82
86
var nextPath = router . getCurrentQuery ( ) . nextPath ;
83
87
var email = this . refs . email . getDOMNode ( ) . value ;
84
88
var pass = this . refs . pass . getDOMNode ( ) . value ;
85
- auth . login ( email , pass , function ( loggedIn ) {
89
+ auth . login ( email , pass , ( loggedIn ) => {
86
90
if ( ! loggedIn )
87
91
return this . setState ( { error : true } ) ;
88
-
89
92
if ( nextPath ) {
90
93
router . replaceWith ( nextPath ) ;
91
94
} else {
92
95
router . replaceWith ( '/about' ) ;
93
96
}
94
- } . bind ( this ) ) ;
95
- } ,
97
+ } ) ;
98
+ }
96
99
97
- render : function ( ) {
98
- var errors = this . state . error ? < p > Bad login information</ p > : '' ;
100
+ render ( ) {
99
101
return (
100
102
< form onSubmit = { this . handleSubmit } >
101
103
< label > < input ref = "email" placeholder = "email" defaultValue = "[email protected] " /> </ label >
102
104
< label > < input ref = "pass" placeholder = "password" /> </ label > (hint: password1)< br />
103
105
< button type = "submit" > login</ button >
104
- { errors }
106
+ { this . state . error && (
107
+ < p > Bad login information</ p >
108
+ ) }
105
109
</ form >
106
110
) ;
107
111
}
108
- } ) ;
112
+ }
113
+
114
+ Login . contextTypes = {
115
+ router : React . PropTypes . func
116
+ } ;
109
117
110
- var About = React . createClass ( {
111
- render : function ( ) {
118
+ class About extends React . Component {
119
+ render ( ) {
112
120
return < h1 > About</ h1 > ;
113
121
}
114
- } ) ;
122
+ }
115
123
116
- var Logout = React . createClass ( {
117
- componentDidMount : function ( ) {
124
+ class Logout extends React . Component {
125
+ componentDidMount ( ) {
118
126
auth . logout ( ) ;
119
- } ,
127
+ }
120
128
121
- render : function ( ) {
129
+ render ( ) {
122
130
return < p > You are now logged out</ p > ;
123
131
}
124
- } ) ;
132
+ }
125
133
126
134
127
135
// Fake authentication lib
128
136
129
137
var auth = {
130
- login : function ( email , pass , cb ) {
138
+ login ( email , pass , cb ) {
131
139
cb = arguments [ arguments . length - 1 ] ;
132
140
if ( localStorage . token ) {
133
141
if ( cb ) cb ( true ) ;
134
142
this . onChange ( true ) ;
135
143
return ;
136
144
}
137
- pretendRequest ( email , pass , function ( res ) {
145
+ pretendRequest ( email , pass , ( res ) => {
138
146
if ( res . authenticated ) {
139
147
localStorage . token = res . token ;
140
148
if ( cb ) cb ( true ) ;
@@ -143,7 +151,7 @@ var auth = {
143
151
if ( cb ) cb ( false ) ;
144
152
this . onChange ( false ) ;
145
153
}
146
- } . bind ( this ) ) ;
154
+ } ) ;
147
155
} ,
148
156
149
157
getToken : function ( ) {
@@ -164,7 +172,7 @@ var auth = {
164
172
} ;
165
173
166
174
function pretendRequest ( email , pass , cb ) {
167
- setTimeout ( function ( ) {
175
+ setTimeout ( ( ) => {
168
176
if ( email === '[email protected] ' && pass === 'password1' ) {
169
177
cb ( {
170
178
authenticated : true ,
0 commit comments