Guest User

Untitled

a guest
Feb 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. router.post('/resetPassword', function(req, res, next) {
  2. async.waterfall([
  3. function(done) {
  4. crypto.randomBytes(25, function(err, buf) {
  5. var token = buf.toString('hex');
  6. done(err, token);
  7. });
  8. },
  9. function(token, done) {
  10. User.findOne({ email: req.body.username}, function(err, user) {
  11. if (!user) {
  12. req.flash('error', 'No account with that email address exists.');
  13. return res.redirect('forgot');
  14. }
  15.  
  16.  
  17. user.resetPasswordToken = token;
  18. user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
  19.  
  20. user.save(function(err) {
  21. done(err, token, user);
  22. });
  23.  
  24. });
  25. },
  26. function(token, user, done) {
  27. var transporter = nodemailer.createTransport({
  28. service: 'gmail',
  29. auth: {
  30. user: emailAddress,
  31. pass: config.emailPass
  32. }
  33. });
  34. var mailOptions = {
  35. to: user.email,
  36. from: emailAddress2,
  37. subject: 'Tracker Password Reset',
  38. text: 'A request has been made to update the password on the tracking site.nn' +
  39. 'Please click on the follow this link to do so:nn' +
  40. 'http://' + req.headers.host + '/users/reset/' + token + 'nn' +
  41. 'If you did not request this, please ignore this email and your password will remain unchanged.n'
  42. };
  43.  
  44. transporter.sendMail(mailOptions, function(err, info){
  45. if (error) {
  46. done(err, 'done');
  47. console.log(error);
  48. } else {
  49. done(err, 'done');
  50. console.log('Email sent: ' + info.response);
  51. }
  52. });
  53. }
  54. ], function(err) {
  55.  
  56. if (err) return next(err);
  57. req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.');
  58. return res.redirect('back');
  59. });
  60. });
Add Comment
Please, Sign In to add comment