-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathuserController.js
More file actions
686 lines (597 loc) · 17.9 KB
/
userController.js
File metadata and controls
686 lines (597 loc) · 17.9 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
var _ = require('lodash')
var async = require('async')
var crypto = require('crypto')
var passport = require('passport')
var User = require('../models/user')
var mailer = require('../lib/mailer')
var helper = require('./controllerHelpers')
/**
* GET /login
* Login page.
*/
exports.getLogin = function(req, res) {
if (req.user)
return res.redirect('/')
res.render('server/pages/account/login', {
meta: {
bodyclass: 'bLogin',
title: 'Login',
scripts: [
helper.metaScript('site/accountpages.js')
]
}
})
}
/**
* POST /login
* Sign in using email and password.
* @param email
* @param password
*/
exports.postLogin = function(req, res, next) {
req.assert('email', 'Email is not valid').isEmail()
req.assert('password', 'Password cannot be blank').notEmpty()
var wantJson = req.xhr || req.path.slice(-5) === '.json'
function returnErrors(errors, status, reason) {
if (wantJson)
return res.status(status || 400).json(helper.responseStatusError(reason, errors))
req.flash('errors', helper.parseErrors(errors))
return res.redirect('/login')
}
var errors = req.validationErrors()
if (errors)
return returnErrors(errors, 400, 'Validation failed')
passport.authenticate('local', function(err, user, info) {
if (err)
return returnErrors([{ message: err.toString() }], 500, 'Server error')
if (!user) {
if (info)
return returnErrors([ info ], 401, 'Failed authentication')
}
req.logIn(user, function(err) {
if (err)
return returnErrors([{ message: err.toString() }], null, 'could not login')
var message = 'Success! You are logged in.'
if (wantJson)
res.json(helper.responseStatusSuccess(message, user.toJSON(), {
redirect: req.session.returnTo || '/account'
}))
else {
req.flash('success', { message: message})
res.redirect(req.session.returnTo || '/account')
}
})
})(req, res, next)
}
/**
* GET /logout
*/
exports.logout = function(req, res) {
req.logout()
res.redirect('/')
}
/**
* GET /signup
*/
exports.getSignup = function(req, res) {
var wantJSON = req.xhr
if (wantJSON) {
if (req.user) {
return res.status(403).json(helper.responseStatusError('user already logged in')).end()
}
return res.status(501).json(helper.responseStatusError('not yet implemented')).end()
} else {
if (req.user) {
return res.redirect('/')
}
res.render('server/pages/account/signup', {
meta : {
title: 'Sign up to Patches',
scripts: [
helper.metaScript('site/accountpages.js')
]
}
})
}
}
/**
* GET /account/exists
**/
exports.checkUserName = function(req, res, next) {
User.findOne({ username: req.body.username },
function(err, existingUser) {
if (err)
return next(err)
if (!existingUser) {
return res.json(
helper.responseStatusSuccess('Username is available',
{username: req.body.username})
)
}
var msg = 'Username taken'
var error = helper.formatResponseError('username', req.body.username, msg)
return res.status(409)
.json(helper.responseStatusError(msg,error))
.end()
}
)
}
/**
* GET /account/email/exists
**/
exports.checkEmailExists = function(req, res, next) {
console.log('checkEmailExists', req.body.email)
User.findOne({ email: req.body.email },
function(err, existingUser) {
if (err)
return next(err)
if (!existingUser) {
return res.json(
helper.responseStatusSuccess('Email is available',
{email: req.body.email})
)
}
var msg = 'Email taken'
var error = helper.formatResponseError('email', req.body.email, msg)
return res.status(409)
.json(helper.responseStatusError(msg, error))
.end()
}
)
}
/**
* POST /signup
* Create a new local account.
* @param email
* @param password
*/
exports.postSignup = function(req, res, next) {
req.sanitize('name').trim()
req.sanitize('username').trim()
req.sanitize('email').trim()
req.assert('name', 'Please enter a name').notEmpty()
req.assert('username', 'Username is empty or invalid').isAlphanumeric()
req.assert('email', 'Email is not valid').isEmail()
req.assert('password', 'Password must be at least 8 characters long').len(8)
var wantJSON = req.xhr;
var errors = req.validationErrors()
if (errors) {
var lastreq = req.body;
delete(lastreq.password);
if (wantJSON)
return res.status(400).json(helper.responseStatusError('Failed validation', errors, {request:lastreq}))
req.flash('errors', helper.parseErrors(errors));
return res.redirect('/signup');
}
var user = new User({
name: req.body.name.replace(/[<>\\\\'\"]+/gim, ''),
username: req.body.username,
email: req.body.email,
password: req.body.password
})
User.findOne({ username: req.body.username }, function(err, existingUser) { // #664
if (existingUser) {
if (req.xhr){
var msg = 'An account with that username already exists.'
var error = helper.formatResponseError('username', req.body.username, msg)
return res.status(400).json(helper.responseStatusError(msg, error))
}
return res.redirect('/signup')
}
User.findOne({ email: req.body.email }, function(err, existingUser) { // #664
if (existingUser) {
if (req.xhr) {
var msg = 'An account with that email already exists.'
var error = helper.formatResponseError('email', req.body.email, msg)
return res.status(400).json(helper.responseStatusError(msg, error))
}
return res.redirect('/signup')
}
user.save(function(err) {
if (err) return next(err)
req.logIn(user, function(err) {
if (err) return next(err)
if (req.xhr) {
res.status(200).json(helper.responseStatusSuccess('New account created', user.toJSON(), {
redirect: req.session.returnTo || '/account'
}))
} else {
res.redirect(req.session.returnTo || '/account')
}
})
})
})
})
}
/**
* GET /account
* redirect to /account/profile
*/
exports.getAccount = function(req, res) {
res.redirect('/account/profile');
}
/**
* GET /account/profile
* Profile page.
*/
exports.getAccountProfile = function(req, res) {
var wantJSON = req.xhr
User.findById(req.user.id, function(err, user) {
if (err) return next(err)
if (wantJSON) {
return res.status(200).json(helper.responseStatusSuccess('OK', user.toJSON()))
}
else {
var data = {
meta: {
title: 'Account Management',
bodyclass: 'bProfile',
header: 'srv/userpage/userpageHeader',
footer: 'srv/home/_footer',
scripts: [
helper.metaScript('site/accountpages.js')
]
},
profile: user.toJSON()
}
res.render('server/pages/account/profile', data)
}
})
}
/**
* POST /account/profile
* Update profile information.
*/
exports.postUpdateProfile = function(req, res, next) {
// to add a key
// 1.sanitize
// 2. check for key in req.body
// 3. req.assert validation by key
// 4. add req.body.key to updateFields.key
// 5. add key to copyKeysIfExist
var mustConfirmPassword = false
req.sanitize('name').trim()
req.sanitize('email').trim()
req.sanitize('profile[website]').trim()
req.sanitize('profile[bio]').trim()
var updateFields = {
profile: {},
preferences: {}
}
if ('email' in req.body) {
mustConfirmPassword = true
req.assert('email', 'Email is not valid').isEmail()
updateFields.email = req.body.email
}
if('password' in req.body) {
mustConfirmPassword = true
req.assert('password', 'New password is not valid').isLength({min: 6})
updateFields.password = req.body.password
}
if ('name' in req.body) {
req.assert('name', 'Name is not valid').notEmpty()
updateFields.name = req.body.name.replace(/[<>\\\\'\"]+/gim, '')
}
if (req.body.profile) {
if ('website' in req.body.profile) {
// allow '' or valid URL only
if (req.body.profile.website !== '')
req.assert('profile[website]', 'Website is not valid').isURL({require_protocol:true})
updateFields.profile.website = req.body.profile.website
}
if ('bio' in req.body.profile) {
req.assert('profile[bio]', 'Bio must be shorter than 200 characters').isLength({min: 0, max: 200})
updateFields.profile.bio = req.body.profile.bio
}
}
if (req.body.preferences) {
if ('publishDefaultPublic' in req.body.preferences)
updateFields.preferences.publishDefaultPublic = (req.body.preferences.publishDefaultPublic === '1')
}
if (mustConfirmPassword)
req.assert('current_password', 'Your current password is required').notEmpty()
var errors = req.validationErrors()
if (errors) {
delete req.body.password
delete req.body.current_password
return helper.respond(req, res, 400, 'Failed validation', errors, null, '/account/profile')
}
function updateUser(user, updateFields) {
function copyKeysIfExist(keys, fromObj, toObj) {
keys.forEach(function(key){
if (key in fromObj)
toObj[key] = fromObj[key]
})
}
copyKeysIfExist(['email', 'password', 'name'], updateFields , user)
copyKeysIfExist(['website', 'bio'] , updateFields.profile, user.profile)
copyKeysIfExist(['publishDefaultPublic'] , updateFields.preferences, user.preferences)
function save() {
user.save(function(err) {
if (err)
return next(err)
return helper.respond(req, res, 200, 'Account details updated', user.toJSON(), null, '/account/profile')
})
}
// if updating email, then check that email does not exist already
if ('email' in updateFields) {
User.findOne({email: updateFields.email}, function(err, existingUser){
if (!existingUser) {
save()
return
}
// else
var error = helper.formatResponseError('email', req.body.email, 'Another account with that email already exists')
return helper.respond(req, res, 409, 'Duplicate email', error, null, '/account/profile')
})
} else
save()
}
User.findById(req.user.id, function(err, user) {
if (err) return next(err)
if (mustConfirmPassword)
user.comparePassword(req.body.current_password, function(err, isMatch) {
if (isMatch)
updateUser(user, updateFields)
else {
return helper.respond(req, res, 401, 'Unauthorised',
helper.formatResponseError('current_password', '', 'Current password does not match'),
null, '/account/profile')
}
})
else
updateUser(user, updateFields)
})
}
/**
* POST /account/password
* Update current password. Used on resetting password from email link
* @param password
* @see this.postUpdateProfile() for updating the password from the profile page
*/
exports.postUpdatePassword = function(req, res, next) {
req.assert('password', 'Password must be at least 8 characters long').len(8)
req.assert('confirm', 'Passwords must match').equals(req.body.password)
var wantJSON = req.xhr
var errors = req.validationErrors()
if (errors) {
if (!wantJSON) {
req.flash('errors', helper.parseErrors(errors))
return res.redirect('/account/profile')
} else {
return res.status(400).json(
helper.responseStatusError('Failed validation', errors) // request intentionally not returned
)
}
}
User.findById(req.user.id, function(err, user) {
if (err) return next(err)
user.password = req.body.password
user.save(function(err) {
if (err) return next(err)
if (wantJSON) {
return res.json(
helper.responseStatusSuccess('Password has been changed.', user.toJSON())
)
} else {
req.flash('success', { message: 'Password has been changed.' })
res.redirect('/account/profile')
}
})
})
}
/**
* POST /account/delete
* Delete user account.
*/
exports.postDeleteAccount = function(req, res, next) {
User.remove({ _id: req.user.id }, function(err) {
if (err) return next(err)
req.logout()
req.flash('info', { message: 'Your account has been deleted.' })
res.redirect('/')
})
}
/**
* GET /account/unlink/:provider
* Unlink OAuth2 provider from the current user.
* @param provider
* @param id - User ObjectId
*/
exports.getOauthUnlink = function(req, res, next) {
var provider = req.params.provider
User.findById(req.user.id, function(err, user) {
if (err) return next(err)
user[provider] = undefined
user.tokens = _.reject(user.tokens, function(token) { return token.kind === provider })
user.save(function(err) {
if (err) return next(err)
req.flash('info', { message: provider + ' account has been unlinked.' })
res.redirect('/account/profile')
})
})
}
/**
* GET /reset/:token
* Reset Password page.
*/
exports.getReset = function(req, res) {
if (req.isAuthenticated()) {
return res.redirect('/')
}
User
.findOne({ resetPasswordToken: req.params.token })
.where('resetPasswordExpires').gt(Date.now())
.exec(function(err, user) {
if (!user) {
req.flash('errors', { message: 'Password reset token is invalid or has expired.' })
return res.redirect('/forgot')
}
res.render('server/pages/account/setPassword', {
meta: {
bodyclass: 'bLogin'
},
title: 'Password Reset',
token: req.params.token
})
})
}
/**
* POST /reset/:token
* Process the reset password request.
*/
exports.postReset = function(req, res, next) {
req.assert('password', 'Password must be at least 8 characters long').len(8)
req.assert('confirm', 'Passwords must match.').equals(req.body.password)
var errors = req.validationErrors()
if (errors) {
req.flash('errors', helper.parseErrors(errors))
return res.redirect('back')
}
async.waterfall([
function(done) {
User
.findOne({ resetPasswordToken: req.params.token })
.where('resetPasswordExpires').gt(Date.now())
.exec(function(err, user) {
if (!user) {
req.flash('errors', { message: 'Password reset token is invalid or has expired.' })
return res.redirect('back')
}
user.password = req.body.password
user.resetPasswordToken = undefined
user.resetPasswordExpires = undefined
user.save(function(err) {
if (err) return next(err)
req.logIn(user, function(err) {
done(err, user)
})
})
})
},
function(user, done) {
var mail = {
to: user.email,
from: 'info@vizor.io',
subject: 'Your Patches password has been changed',
html: 'Hello,<br><br>' +
'This is a confirmation that the password for your account ' + user.email + ' has just been changed.<br><br>',
text: 'Hello,\n\n' +
'This is a confirmation that the password for your account ' + user.email + ' has just been changed.\n',
}
mailer.send(mail.to, mail.subject, mail.html, mail.text)
.then(function() {
req.flash('success', { message: 'Success! Your password has been changed.' })
done()
})
.catch(done)
}
], function(err) {
if (err) return next(err)
res.redirect('/')
})
}
/**
* GET /forgot
* Forgot Password page.
*/
exports.getForgot = function(req, res) {
if (req.isAuthenticated()) {
return res.redirect('/account')
}
res.render('server/pages/account/forgotPassword', {
meta: {
title: 'Forgot Password',
bodyclass: 'bAccount'
}
})
}
/**
* POST /forgot
* Create a random token, then the send user an email with a reset link.
* @param email
*/
exports.postForgot = function(req, res, next) {
req.sanitize('email').trim()
req.assert('email', 'Please enter a valid email address.').isEmail()
var wantJSON = req.xhr
var errors = req.validationErrors()
if (errors) {
if (wantJSON) {
return res.status(400).json(
helper.responseStatusError('Failed validation', errors, {request:req.body})
)
} else {
req.flash('errors', helper.parseErrors(errors))
return res.redirect('/forgot')
}
}
var email = req.body.email
async.waterfall([
function(done) {
crypto.randomBytes(16, function(err, buf) {
var token = buf.toString('hex')
done(err, token)
})
},
function(token, done) {
User.findOne({ email: req.body.email }, function(err, user) { // #664
if (!user) {
var msg = 'No account with that email address exists.'
if (wantJSON) {
return res.status(400).json(
helper.responseStatusError(msg, helper.formatResponseError('email', email, msg), {request: req.body})
)
} else {
req.flash('errors', { message: msg })
return res.redirect('/forgot')
}
}
user.resetPasswordToken = token
user.resetPasswordExpires = Date.now() + 3600000 // 1 hour
user.save(function(err) {
done(err, token, user)
})
})
},
function(token, user, done) {
var mail = {
to: user.email,
from: 'info@vizor.io',
subject: 'Reset your Patches password',
html: 'You are receiving this email because you (or someone else) have requested the reset of the password for your account.<br/><br/>' +
'Please click on the following link, or paste this into your browser to complete the process:<br/><br/>' +
'http://' + req.headers.host + '/reset/' + token + '<br/><br/>' +
'If you did not request this, please ignore this email and your password will remain unchanged.<br/><br/>',
text: 'You are receiving this email because you (or someone else) have requested the reset of the password for your account.\n\n' +
'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
'http://' + req.headers.host + '/reset/' + token + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n'
}
mailer.send(mail.to, mail.subject, mail.html, mail.text)
.then(function() {
if (!wantJSON) {
req.flash('info', {
message: 'We emailed further instructions to ' + user.email + '.'
})
}
done()
})
.catch(done)
}
], function(err) {
if (err) {
res.status(500).json(
helper.responseStatusError('The server could not email you. Please contact us for assistance.', [], {diag: err})
)
return next(err)
}
if (wantJSON) {
return res.status(200).json(
helper.responseStatusSuccess('We emailed further instructions to ' + email + '.', {email: email})
)
} else {
res.redirect('/forgot')
}
})
}