我正在使用passport.js和mailgun-validate-email尝试验证电子邮件,然后根据该电子邮件创建用户帐户。我可以让它来验证电子邮件,但它不会完成POST
实际创建用户的请求。它最终会显示一个帖子日志,但它甚至不会给出一个我发现很奇怪的状态代码。我正在使用morgan来查看此内容。通常它确实如此,但这次只有-
状态代码。
Passport.use注册中间件
passport.use('signup', new LocalStrategy({
usernameField: 'email',
passReqToCallback : true
},
function(req, email, password, done) {
var findOrCreateUser = function(){
console.log(req.body.email);
User.findOne({ email: req.body.email }, function(err, existingUser) {
if(err){
console.log(err);
}
if (existingUser) {
req.flash('form', {
email: req.body.email
});
return done(null, false, req.flash('error', 'An account with that email address already exists.'));
}
// edit this portion to accept other properties when creating a user.
var user = new User({
email: req.body.email,
password: req.body.password // user schema pre save task hashes this password
});
user.save(function(err) {
if (err) return done(err, false, req.flash('error', 'Error saving user.'));
var token = new Token({ _userId: user._id, token: crypto.randomBytes(16).toString('hex') });
token.save(function (err) {
if (err) return done(null, false, req.flash('error', err.message));
var email = req.body.email;
// Send the email
var message = 'Hello,\n\n' + 'Please verify your account by clicking the link: \nhttp:\/\/' + req.headers.host + '\/confirmation\/' + token.token + '\/' + email + '\n';
sendEmail('"Phantom Asset Management" noreply@phantomam.com', user.email, 'Account Verification Token', message);
});
var time = 14 * 24 * 3600000;
req.session.cookie.maxAge = time; //2 weeks
req.session.cookie.expires = new Date(Date.now() + time);
req.session.touch();
return done(null, user, req.flash('success', 'A verification email has been sent to ' + user.email + '.'));
});
console.log('done');
});
};
process.nextTick(findOrCreateUser);
})
);
然后我的控制器
exports.postSignup = function(req, res, next){
req.assert('email', 'Please sign up with a valid email.').isEmail();
req.assert('password', 'Password must be at least 6 characters long').len(6);
var errors = req.validationErrors();
if (errors) {
req.flash('errors', errors);
req.flash('form', {
email: req.body.email
});
return res.redirect('/signup');
}
validator(req.body.email, function (err, result){
if(err) {
return console.log('Error: ', err);
} else {
console.log('Result: ', result);
if(result.is_valid == false){
req.flash('error', 'Looks like your email is not valid did you mean ' + result.did_you_mean + '?');
return res.redirect(req.redirect.failure);
} else if(result.is_valid == true) {
// calls next middleware to authenticate with passport
passport.authenticate('signup', {
successRedirect: '/dashboard', // Select redirect for post signup
failureRedirect: '/signup',
failureFlash : true
});
}
}
});
(req, res, next);
next();
};
如果我理解我的方法是正确的,首先检查以确保它是电子邮件,然后密码是正确的长度。如果有错误,则会让用户知道并将其重定向回/signup
。然后它进入验证器并向控制台发出错误或检查它是否有效。如果它无效则向用户显示错误但如果有效则进入我的护照中间件。
哪个应该采取并检查用户是否存在然后需要标记错误。它保存用户和该用户的令牌,以用于验证其电子邮件地址。这一切都很好,期望保存用户和令牌。
看起来它可能是request.js
问题,我得到error: esockettimedout
。 mailgun-validate-email
的唯一依赖项仅为request 2.25.0
。 esockettimedout
位于request.js:813:19