简体   繁体   English

在发布路由时,请使用nodemailer发送电子邮件,并使用通行证将用户保存到mongodb。

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

I am trying to send a welcome email after a user signs up with nodemailer and also adding the user to mongodb with passport.authenticate on the same post route. 我试图在用户使用nodemailer注册后发送欢迎电子邮件,并且还将用户在同一发布路线上使用passport.authenticate将用户添加到mongodb中。 I am able to get this to work separately ie either sending email or adding the user to the database but can't seem to get them to work together. 我能够使它单独工作,即发送电子邮件或将用户添加到数据库,但似乎无法使他们一起工作。 I am new to nodejs and would really appreciate any help. 我是nodejs的新手,非常感谢您的帮助。 Here is the route I am trying to get to work: 这是我尝试上班的路线:

  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');
  });
});

Here is the signup strategy with passport: 这是使用护照的注册策略:

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);
    }

}

Thanks in advance for the help! 先谢谢您的帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM