forked from btouellette/concarneau
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
265 lines (222 loc) · 8.71 KB
/
routes.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
var User = require('./models/user');
const {parse, stringify} = require('flatted/cjs');
//TODO: confirm e-mail before allowing into game
module.exports = function(app, passport, client) {
// normal routes ===============================================================
// show the home page (will also have our login links)
app.get('/', function(req, res) {
if(req.isAuthenticated()) {
if(process.env.SENTRY_DSN) { client.setUserContext({ user: req.user}); }
res.redirect('/game');
} else {
res.render('index.ejs');
}
});
// PROFILE SECTION =========================
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user : req.user
});
});
// LOGOUT ==============================
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.get('/username', isLoggedIn, function(req, res) {
if(req.user.username) {
res.redirect('/game');
} else {
res.render('username.ejs', {
user : req.user,
message: req.flash('usernameMessage')
});
}
});
// process the username form
app.post('/username', isLoggedIn, function(req, res) {
console.log(req.headers['x-request-id'] + ' - selecting new username - ' + stringify(req));
if(!req.user.username) {
var username = req.body.username.toLowerCase();
console.log(req.headers['x-request-id'] + ' - testing valid');
if(/^[a-z0-9_]{1,12}$/.test(username)) {
console.log(req.headers['x-request-id'] + ' - setting username');
User.findByIdAndUpdate(req.user._id, { $set: { username: username }}, function(err) {
console.log(req.headers['x-request-id'] + ' - setting username callback');
if(err) {
console.log(req.headers['x-request-id'] + ' - username callback error - ' + stringify(err));
if(err.lastErrorObject && err.lastErrorObject.code === 11001) {
console.log(req.headers['x-request-id'] + ' - setting username callback error taken');
req.flash('usernameMessage', 'Username already taken');
} else {
console.log(req.headers['x-request-id'] + ' - setting username callback error unknown');
req.flash('usernameMessage', err.errmsg);
}
console.log(req.headers['x-request-id'] + ' - reloading username');
res.redirect('/username');
} else {
console.log(req.headers['x-request-id'] + ' - set user name sending to game');
res.redirect('/game');
}
});
} else {
console.log(req.headers['x-request-id'] + ' - not valid');
req.flash('usernameMessage', username.length > 12 ? 'Username too long' : username.length === 0 ? 'Username too short' : 'Username using invalid characters');
res.redirect('/username');
}
} else {
console.log(req.headers['x-request-id'] + ' - already has username');
res.redirect('/game');
}
});
app.get('/game', [isLoggedIn, hasUsername], function(req, res) {
if(process.env.SENTRY_DSN) { client.setUserContext({ user: req.user}); }
res.render('game.ejs', {
user : req.user
});
});
// =============================================================================
// AUTHENTICATE (FIRST LOGIN) ==================================================
// =============================================================================
// locally --------------------------------
// LOGIN ===============================
// show the login form
app.get('/login', function(req, res) {
res.render('login.ejs', { message: req.flash('loginMessage') });
});
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/game', // redirect to the secure game section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// SIGNUP =================================
// show the signup form
app.get('/signup', function(req, res) {
res.render('signup.ejs', { message: req.flash('signupMessage') });
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/username', // redirect to the username choosing page
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// facebook -------------------------------
// send to facebook to do the authentication
app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
// handle the callback after facebook has authenticated the user
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/game',
failureRedirect : '/'
}));
// twitter --------------------------------
// send to twitter to do the authentication
app.get('/auth/twitter', passport.authenticate('twitter', { scope : 'email' }));
// handle the callback after twitter has authenticated the user
app.get('/auth/twitter/callback',
passport.authenticate('twitter', {
successRedirect : '/game',
failureRedirect : '/'
}));
// google ---------------------------------
// send to google to do the authentication
app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] }));
// the callback after google has authenticated the user
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect : '/game',
failureRedirect : '/'
}));
// =============================================================================
// AUTHORIZE (ALREADY LOGGED IN / CONNECTING OTHER SOCIAL ACCOUNT) =============
// =============================================================================
// locally --------------------------------
app.get('/connect/local', function(req, res) {
res.render('connect-local.ejs', { message: req.flash('loginMessage') });
});
app.post('/connect/local', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/connect/local', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// facebook -------------------------------
// send to facebook to do the authentication
app.get('/connect/facebook', passport.authorize('facebook', { scope : ['email'] }));
// handle the callback after facebook has authorized the user
app.get('/connect/facebook/callback',
passport.authorize('facebook', {
successRedirect : '/profile',
failureRedirect : '/'
}));
// twitter --------------------------------
// send to twitter to do the authentication
app.get('/connect/twitter', passport.authorize('twitter', { scope : ['email'] }));
// handle the callback after twitter has authorized the user
app.get('/connect/twitter/callback',
passport.authorize('twitter', {
successRedirect : '/profile',
failureRedirect : '/'
}));
// google ---------------------------------
// send to google to do the authentication
app.get('/connect/google', passport.authorize('google', { scope : ['profile', 'email'] }));
// the callback after google has authorized the user
app.get('/connect/google/callback',
passport.authorize('google', {
successRedirect : '/profile',
failureRedirect : '/'
}));
// =============================================================================
// UNLINK ACCOUNTS =============================================================
// =============================================================================
// used to unlink accounts. for social accounts, just remove the token
// for local account, remove email and password
// user account will stay active in case they want to reconnect in the future
// local -----------------------------------
app.get('/unlink/local', function(req, res) {
var user = req.user;
user.local.email = undefined;
user.local.password = undefined;
user.save(function(err) {
res.redirect('/profile');
});
});
// facebook -------------------------------
app.get('/unlink/facebook', function(req, res) {
var user = req.user;
user.facebook.token = undefined;
user.save(function(err) {
res.redirect('/profile');
});
});
// twitter --------------------------------
app.get('/unlink/twitter', function(req, res) {
var user = req.user;
user.twitter.token = undefined;
user.save(function(err) {
res.redirect('/profile');
});
});
// google ---------------------------------
app.get('/unlink/google', function(req, res) {
var user = req.user;
user.google.token = undefined;
user.save(function(err) {
res.redirect('/profile');
});
});
};
// route middleware to ensure user is logged in
function isLoggedIn(req, res, next) {
if(req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
function hasUsername(req, res, next) {
if(req.user.username) {
return next();
}
res.redirect('/username');
}