我正在尝试将google reCaptcha合并到我已经拥有护照身份验证的注册表单的后端,代码如下所示:
app.get('/signup', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('signup.ejs', {
message : req.flash('signupMessage')
});
});
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/app', // redirect to the secure chat section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
这是我的google recaptha代码:
app.get('/signup', recaptcha.middleware.render, function(req, res) {
// render the page and pass in any flash data if it exists
res.render('signup.ejs', {
message : req.flash('signupMessage'),
captcha : req.recaptcha
});
});
app.post('/signup', recaptcha.middleware.verify, function(req, res){
if (!req.recaptcha.error) {
//if recaptcha is correct do somthing
}else {
req.flash('signupMessage','reCAPTCHA Incorrect');
res.redirect('/signup');
}
});
两者都独立工作,但当我将它们组合起来时:
app.get('/signup', isNotLoggedIn, recaptcha.middleware.render, function(req, res) {
// render the page and pass in any flash data if it exists
res.render('signup.ejs', {
message : req.flash('signupMessage'),
captcha : req.recaptcha
});
});
app.post('/signup', recaptcha.middleware.verify, function(req, res){
if (!req.recaptcha.error) {
passport.authenticate('local-signup', {
successRedirect : '/app',
failureRedirect : '/signup',
failureFlash : true
});
}else {
req.flash('signupMessage','reCAPTCHA Incorrect');
res.redirect('/signup');
}
});
或者这个:
app.get('/signup', isNotLoggedIn, recaptcha.middleware.render, function(req, res) {
// render the page and pass in any flash data if it exists
res.render('signup.ejs', {
message : req.flash('signupMessage'),
captcha : req.recaptcha
});
});
app.post('/signup', recaptcha.middleware.verify, function(req, res){
if (req.recaptcha.error) {
req.flash('signupMessage','reCAPTCHA Incorrect');
res.redirect('/signup');
}
},
passport.authenticate('local-signup', {
successRedirect : '/app', // redirect to the secure chat section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
页面一直在加载,没有任何事情发生。
答案 0 :(得分:2)
所以我想出来了!
诀窍在于使用next();如果验证码是正确的函数,所以代码最终会像这样:
app.get('/signup', isNotLoggedIn, recaptcha.middleware.render, function(req, res) {
// render the page and pass in any flash data if it exists
res.render('signup.ejs', {
message : req.flash('signupMessage'),
captcha : req.recaptcha
});
});
app.post('/signup', recaptcha.middleware.verify, captchaVerification, passport.authenticate('local-signup', {
successRedirect : '/app', // redirect to the secure chat section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
function captchaVerification(req, res, next) {
if (req.recaptcha.error) {
req.flash('signupMessage','reCAPTCHA Incorrect');
res.redirect('/signup');
} else {
return next();
}
}
这就是你如何将google验证码集成到你的nodejs,express,passport,后端