Node.js 创建帐户电子邮件验证不起作用
我正在尝试这样,当用户注册到我的网站时,他们会收到一封电子邮件,说他们需要验证他们的电子邮件才能使用该网站。它当前发送带有令牌的电子邮件很好,但当我转到确认链接时,它会失败,状态为404 路线Node.js 创建帐户电子邮件验证不起作用,node.js,express,passport.js,email-verification,Node.js,Express,Passport.js,Email Verification,我正在尝试这样,当用户注册到我的网站时,他们会收到一封电子邮件,说他们需要验证他们的电子邮件才能使用该网站。它当前发送带有令牌的电子邮件很好,但当我转到确认链接时,它会失败,状态为404 路线 app.post('/confirmation/:token', users.confirmationPost); 需要控制器 var users = require('../controllers/users-controller'); 确认帖子 exports.confirmationPos
app.post('/confirmation/:token',
users.confirmationPost);
需要控制器
var users = require('../controllers/users-controller');
确认帖子
exports.confirmationPost = function (req, res, next) {
req.assert('email', 'Email is not valid').isEmail();
req.assert('email', 'Email cannot be blank').notEmpty();
req.assert('token', 'Token cannot be blank').notEmpty();
req.sanitize('email').normalizeEmail({ remove_dots: false });
// Check for validation errors
var errors = req.validationErrors();
if (errors) return res.status(400).send(errors);
// Find a matching token
Token.findOne({ token: req.body.token }, function (err, token) {
if(err) throw err;
if (!token) return res.status(400).send({ type: 'not-verified', msg: 'We were unable to find a valid token. Your token my have expired.' });
// If we found a token, find a matching user
User.findOne({ _id: token._userId }, function (err, user) {
if(err) throw err;
if (!user) return res.status(400).send({ msg: 'We were unable to find a user for this token.' });
if (user.isVerified) return res.status(400).send({ type: 'already-verified', msg: 'This user has already been verified.' });
// Verify and save the user
user.isVerified = true;
user.save(function (err) {
if (err) { return res.status(500).send({ msg: err.message }); }
res.status(200).send("The account has been verified. Please log in.");
});
});
});
};
护照注册
passport.use('signup', new LocalStrategy({
usernameField: 'email',
passReqToCallback : true
},
function(req, email, password, done) {
var findOrCreateUser = function(){
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));
// Send the email
var message = 'Hello,\n\n' + 'Please verify your account by clicking the link: \nhttp:\/\/' + req.headers.host + '\/confirmation\/' + token.token + '.\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 + '.'));
});
});
};
process.nextTick(findOrCreateUser);
})
);
当我转到
/confirmation/:token
时,我得到了错误404页面,控制台中没有任何内容。我唯一看到的是我的调试器显示了该路由的错误404。这是一个POST
。你确定你发送的邮件请求没有收到吗?。。。嗯。。。我如何通过电子邮件//链接做到这一点?我使用sendmail
功能发送它,实际发送的是var消息。我如何确保/confirmation
路由在用户跟随链接时获得电子邮件和它需要的令牌,这并没有告诉我们多少,但似乎您甚至没有使用POST
调用端点,因此没有调用sendmail
函数。您需要准确地显示如何调用此端点。我们没有您的url来为您测试,所以您需要显示它是如何被调用的。如果安装了curl
,请打开命令提示符并键入curl-X POST/confirmation/1234
。用输出更新您的问题。最后创建一个并用它更新您的帖子。使用PostMan,当我发送一个帖子请求时,我会得到这个响应[{“param”:“email”,“msg”:“email无效”},{“param”:“email”,“msg”:“email不能为空”}]
我知道我需要发送一些数据,但不知道如何发送。直到今天我才使用过邮递员我遵循了这个指南