在 post 路由上使用 nodemailer 发送电子邮件并使用 passport.authenticate 将用户保存到 mongodb

On post route send email with nodemailer and save user to mongodb with passport.authenticate

我正在尝试在用户注册 nodemailer 后发送一封欢迎电子邮件,并在同一 post 路由上使用 passport.authenticate 将用户添加到 mongodb。我能够让它单独工作,即发送电子邮件或将用户添加到数据库,但似乎无法让它们一起工作。我是 nodejs 的新手,非常感谢任何帮助。这是我尝试去上班的路线:

  router.post('/signup', function(req, res,next) {
  async.waterfall([
    function(done) {
      passport.authenticate('signup', {
            successRedirect: '/',
            failureRedirect: '/signup',
            failureFlash : true
        });
    },
    function(user, done) {
      var transporter = nodeMailer.createTransport({
        service: 'SendGrid',
        auth: {
          user: 'user',
          pass: 'password'
        }
      });
      var mailOptions = {
        to: user.email,
        from: 'me@gmail.com',
        subject: 'Welcome to the site',
        html: '<p> This is html, did I render correctly?</p>'
      };
      transporter.sendMail(mailOptions, function(err){
        done(err);
      });
    }
  ], function(err) {
    res.redirect('/signup');
  });
});

以下是护照注册攻略:

var LocalStrategy   = require('passport-local').Strategy;
var User = require('../models/user');
var bCrypt = require('bcrypt-nodejs');


module.exports = function(passport){

    passport.use('signup', new LocalStrategy({
            usernameField : 'email',
            passReqToCallback : true
        },
        function(req, email, password, done) {

            findOrCreateUser = function(){
                // find a user in Mongo with provided username
                User.findOne({ 'email' :  email }, function(err, user) {
                    // In case of any error, return using the done method
                    if (err){
                        req.flash('error','Email Already Exists',err.message);
                        return done(err);
                    }
                    // already exists
                    if (user) {
                        console.log('User already exists with username:');
                        return done(null, false, req.flash('error','Email Already Exists'));
                    } else {
                        // if there is no user with that email
                        // create the user
                        var newUser = new User();

                        // set the user's local credentials
                        newUser.password = createHash(password);
                        newUser.email = req.param('email');
                        newUser.firstName = req.param('firstName');
                        newUser.lastName = req.param('lastName');

                        // save the user
                        newUser.save(function(err) {
                            if (err){
                                console.log('Error in Saving user: '+err);
                                return done(null, false, req.flash('error',err.message));
                            }
                            console.log('User Registration succesful');
                            return done(null, newUser);
                        });
                    }
                });
            };
            // Delay the execution of findOrCreateUser and execute the method
            // in the next tick of the event loop
            process.nextTick(findOrCreateUser);
        })
    );

    // Generates hash using bCrypt
    var createHash = function(password){
        return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
    }

}

在此先感谢您的帮助!

为什么不将电子邮件发送逻辑移至护照注册策略?