Skip to content

Commit 01dfe24

Browse files
committed
convert auth flow to ES6
closes #913
1 parent 6815818 commit 01dfe24

File tree

1 file changed

+64
-56
lines changed

1 file changed

+64
-56
lines changed

examples/auth-flow/app.js

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,64 @@ var React = require('react');
22
var Router = require('react-router');
33
var { Route, RouteHandler, Link } = Router;
44

5-
var App = React.createClass({
6-
getInitialState: function () {
7-
return {
5+
class App extends React.Component {
6+
constructor () {
7+
this.state = {
88
loggedIn: auth.loggedIn()
99
};
10-
},
10+
}
1111

12-
setStateOnAuth: function (loggedIn) {
12+
setStateOnAuth (loggedIn) {
1313
this.setState({
1414
loggedIn: loggedIn
1515
});
16-
},
16+
}
1717

18-
componentWillMount: function () {
19-
auth.onChange = this.setStateOnAuth;
18+
componentWillMount () {
19+
auth.onChange = this.setStateOnAuth.bind(this);
2020
auth.login();
21-
},
21+
}
2222

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 () {
2724
return (
2825
<div>
2926
<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>
3134
<li><Link to="about">About</Link></li>
3235
<li><Link to="dashboard">Dashboard</Link> (authenticated)</li>
3336
</ul>
3437
<RouteHandler/>
3538
</div>
3639
);
3740
}
38-
});
41+
}
3942

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}/>
4847
}
4948
}
50-
};
5149

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+
};
5460

55-
render: function () {
61+
var Dashboard = requireAuth(class extends React.Component {
62+
render () {
5663
var token = auth.getToken();
5764
return (
5865
<div>
@@ -64,77 +71,78 @@ var Dashboard = React.createClass({
6471
}
6572
});
6673

67-
var Login = React.createClass({
68-
69-
contextTypes: {
70-
router: React.PropTypes.func
71-
},
74+
class Login extends React.Component {
7275

73-
getInitialState: function () {
74-
return {
76+
constructor () {
77+
this.handleSubmit = this.handleSubmit.bind(this);
78+
this.state = {
7579
error: false
7680
};
77-
},
81+
}
7882

79-
handleSubmit: function (event) {
83+
handleSubmit (event) {
8084
event.preventDefault();
8185
var { router } = this.context;
8286
var nextPath = router.getCurrentQuery().nextPath;
8387
var email = this.refs.email.getDOMNode().value;
8488
var pass = this.refs.pass.getDOMNode().value;
85-
auth.login(email, pass, function (loggedIn) {
89+
auth.login(email, pass, (loggedIn) => {
8690
if (!loggedIn)
8791
return this.setState({ error: true });
88-
8992
if (nextPath) {
9093
router.replaceWith(nextPath);
9194
} else {
9295
router.replaceWith('/about');
9396
}
94-
}.bind(this));
95-
},
97+
});
98+
}
9699

97-
render: function () {
98-
var errors = this.state.error ? <p>Bad login information</p> : '';
100+
render () {
99101
return (
100102
<form onSubmit={this.handleSubmit}>
101103
<label><input ref="email" placeholder="email" defaultValue="[email protected]"/></label>
102104
<label><input ref="pass" placeholder="password"/></label> (hint: password1)<br/>
103105
<button type="submit">login</button>
104-
{errors}
106+
{this.state.error && (
107+
<p>Bad login information</p>
108+
)}
105109
</form>
106110
);
107111
}
108-
});
112+
}
113+
114+
Login.contextTypes = {
115+
router: React.PropTypes.func
116+
};
109117

110-
var About = React.createClass({
111-
render: function () {
118+
class About extends React.Component {
119+
render () {
112120
return <h1>About</h1>;
113121
}
114-
});
122+
}
115123

116-
var Logout = React.createClass({
117-
componentDidMount: function () {
124+
class Logout extends React.Component {
125+
componentDidMount () {
118126
auth.logout();
119-
},
127+
}
120128

121-
render: function () {
129+
render () {
122130
return <p>You are now logged out</p>;
123131
}
124-
});
132+
}
125133

126134

127135
// Fake authentication lib
128136

129137
var auth = {
130-
login: function (email, pass, cb) {
138+
login (email, pass, cb) {
131139
cb = arguments[arguments.length - 1];
132140
if (localStorage.token) {
133141
if (cb) cb(true);
134142
this.onChange(true);
135143
return;
136144
}
137-
pretendRequest(email, pass, function (res) {
145+
pretendRequest(email, pass, (res) => {
138146
if (res.authenticated) {
139147
localStorage.token = res.token;
140148
if (cb) cb(true);
@@ -143,7 +151,7 @@ var auth = {
143151
if (cb) cb(false);
144152
this.onChange(false);
145153
}
146-
}.bind(this));
154+
});
147155
},
148156

149157
getToken: function () {
@@ -164,7 +172,7 @@ var auth = {
164172
};
165173

166174
function pretendRequest(email, pass, cb) {
167-
setTimeout(function () {
175+
setTimeout(() => {
168176
if (email === '[email protected]' && pass === 'password1') {
169177
cb({
170178
authenticated: true,

0 commit comments

Comments
 (0)