forked from remix-run/react-router
/
app.js
265 lines (245 loc) · 6.3 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import React, { findDOMNode } from 'react';
import { Router, Route, Link } from 'react-router';
import HashHistory from 'react-router/lib/HashHistory';
//pretend server
function loginRequest(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});
}
}, 200);
}
function getUserDataRequest(token, cb) {
setTimeout(() => {
//assume tokens do not expire for test purposes
if(token){
cb({
name: 'Joe Bloggs',
creditcard: '1234567890'
})
}
}, 500);
}
// next path store, default to dashboard
var nextPathStore = '/dashboard';
//token name - NB token value is stored as a string too
var tokenName = 'jwt_test'
// auth module
var auth = (function(){
var loggedIn = false, asyncLoggingIn = false, user = null, self = {
login: function (email, pass, cb) {
asyncLoggingIn = true;
loginRequest(email, pass, (res) => {
if (res.authenticated) {
self.getUserData(res.token);
} else {
self.logout();
cb (loggedIn);
}
});
},
getUserData: function(token){
asyncLoggingIn = true;
getUserDataRequest(token, (res) => {
if(res.error){
self.logout();
}else{
user = res;
localStorage.setItem(tokenName, token);
loggedIn = true;
asyncLoggingIn = false;
self.onChange(loggedIn);
}
})
},
logout: function () {
asyncLoggingIn = false;
loggedIn = false;
user = null;
localStorage.setItem(tokenName, '');
self.onChange(loggedIn);
},
isLoggedIn: function () {
return loggedIn;
},
isAsyncLoggingIn: function () {
return asyncLoggingIn;
},
getUser: function (){
return user;
},
autoLogin: function (){
var token = localStorage.getItem(tokenName);
if (token) {
self.getUserData(token);
}
},
//binds to listener in App component
onChange: function () {}
};
return self;
})();
//authenticated component hook
var requireAuth = function(nextState, transition) {
if (!auth.isLoggedIn()){
//store the path
nextPathStore = nextState.location.pathname
//redirect to the shim
transition.to('/shim');
}
}
//App component
var App = React.createClass({
statics: {
onEnter: function(nextState, transition) {
if(nextState.location.pathname === '/'){
//always attempt to go to the dashboard
transition.to('/dashboard');
}
}
},
contextTypes: {
router: React.PropTypes.object.isRequired
},
getInitialState() {
return {
loggedIn: auth.isLoggedIn()
};
},
//auth login/logout module listener
authListener(loggedIn) {
this.setState({
loggedIn: loggedIn
});
//re-direct post login or logout
if(loggedIn){
console.log('in App authListener, relocating to', nextPathStore);
this.context.router.transitionTo(nextPathStore);
}else{
console.log('in App authListener, relocating to login');
this.context.router.transitionTo('/login');
}
},
componentWillMount() {
//bind
auth.onChange = this.authListener;
},
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>
{this.props.children}
</div>
);
}
});
//dashboard component requires authentication
var Dashboard = React.createClass({
render() {
var user = auth.getUser();
return (
<div>
<h1>Dashboard</h1>
<p>Hi {user.name}. You made it!</p>
<p>Your credit card is {user.creditcard}</p>
</div>
);
}
});
var Login = React.createClass({
contextTypes: {
router: React.PropTypes.object.isRequired
},
getInitialState() {
return {
error: false
};
},
handleSubmit(event) {
event.preventDefault();
var email = findDOMNode(this.refs.email).value;
var pass = findDOMNode(this.refs.pass).value;
//in Flux this should just kick off an action
auth.login(email, pass, (loggedIn) => {
if (!loggedIn)
return this.setState({ error: true });
});
},
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>
);
}
});
// YOU SHOULD ONLY GO HERE WHEN NOT LOGGED IN
var Shim = React.createClass({
contextTypes: {
router: React.PropTypes.object.isRequired
},
transitionCheck(){
var router = this.context.router;
//if not waiting for some async login outcome then re-direct to login
if (!auth.isAsyncLoggingIn()){
console.log('in shim redirecting to login');
router.transitionTo('/login');
}
//if you've gone here by accident
if(auth.isLoggedIn()){
console.log('in shim by accident, redirecting to', nextPathStore);
router.transitionTo(nextPathStore);
}
},
componentWillMount() {
this.transitionCheck();
},
render() {
return <div>waiting...</div>;
}
});
var Logout = React.createClass({
componentDidMount(){
auth.logout();
},
render() {
return <p>You are now logged out</p>;
}
});
var About = React.createClass({
render() {
return <h1>About</h1>;
}
});
auth.autoLogin();
React.render((
<Router history={new HashHistory({ queryKey: true })}>
<Route path="/" component={App} onEnter={App.onEnter}>
<Route path="login" component={Login}/>
<Route path="logout" component={Logout}/>
<Route path="shim" component={Shim}/>
<Route path="about" component={About}/>
<Route path="dashboard" component={Dashboard} onEnter={requireAuth}/>
</Route>
</Router>
), document.getElementById('example'));