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