title: How do I set up reset-password functionality using node.js?
body: <p>Please I need help writing my forgot-password logic. I have tested what I have using Postman but its not working. I am using nodemailer as the mailserver engine .The entire project is been wired using Angular 4/5 (suggested solutions should please consider this factor). Please help check through and give recommendations/corrections. Thanks.</p>
  
  <p>Here is the code so far--</p>
  
  <p>//forgot password</p>
  
  <pre><code>const transport  = require('nodemailer-smtp-transport');
  app.get('/forgotPassword', function(req, res){
  
      });
  app.post('/forgotPassword', function(req, res, next){
      async.waterfall([
          function(done) {
              crypto.randomBytes(20, function(err, buf) {
                  const token = buf.toString('hex');
                  done(err, token);
              });
          },
          function(token, done) {
              user.findOne({email: req.body.email }, function(err, user, req) {
                  if (!user){
                      console.log ('No account with that email address exists.');
  
                  }
                  user.resetPasswordToken = token;
                  user.resetPasswordExpires = Date.now() + 3600000; //1 hour
  
              });
          },
          function(token, user, done){
              const smtpTransporter = nodemailer.createTransport({
                  service: 'Gmail',
                  auth: {
                      user: 'preciousegunjobi@gmail.com',
                      pass: 'Presidoe2011!'
                  }
              });
  
              const mailOptions = {
                  from: 'preciousegunjobi1@gmail.com', //sender
                  to: req.body.email, 
                  subject: 'Password Reset Notification',
                  html: '&lt;h3&gt;You are receiving this because you (or someone else) have requested the reset of the password on your account. ' + '\n' + ' Please click on the following link, or paste into your browser to complete the password reset process. &lt;/h3&gt;' +' \n '+ 'http://' + req.headers.host + '/reset/' + token + '\n\n' +
                         '&lt;h4&gt; If you did not request this, please ignore this email and your password will remain unchanged &lt;/h4&gt;'
              }; 
  
              smtpTransporter.sendMail(mailOptions, function(err, req){
                  console.log('mail sent');
                  console.log ('success', 'An e-mail has been sent to ' + user.email + 'with further instructions to reset password.');
                  done(err, 'done');
              });
  
          }
      ], function(err){
          if (err) return next(err);
  
      });
  });
  app.get ('/reset/:token', function(req, res){
      user.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: {$gt: Date.now() } }, function(err, user, req){
          if (!user){
              console.log ('error', 'Password reset token is invalid or has expired.');
  
          }
         // if(req.body.password === req.body.confirm) {
             // user.setPassword(req.body.password, function(err, token) {
                  user.password = req.body.password;
                  user.resetPasswordToken = undefined;
                  user.resetPasswordExpires = undefined;
                  console.log('password' + user.password + 'and the user is' + user)
  
                //  )
  
              })
         // } else {
              //console.log ("error", "Passwords do not match.");
  
         // }
  // });
  },
  function(user, done) {
     const smtpTransporter = nodemailer.createTransport({
         service: 'Gmail',
         auth: {
             user: 'preciousegunjobi1@gmail.com',
             pass: 'Presidoe2011!'
         }
     });
     const mailOptions = {
         to: user.email,
         from: 'preciousegunjobi1@gmail.com',
         subject: 'Your password has been changed',
         text: 'Hello, \n\n' +
          'This is a confirmation that the password for your account ' + user.email + ' has just been changed.'
     };
     smtpTransporter.sendMail(mailOptions, function(err, req) {
         console.log ('success', 'Success! Your password has been changed.');
         done(err);
     });
  });
  
  //end of forgot password logic
  </code></pre>