Reputation: 554
I'm trying to incorporate google reCaptcha to my signup form's back-end that already has passport authentication on it the code looks like this:
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
}));
And this is my google recaptha code:
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');
}
});
Both work independently, But when I combine them like this:
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');
}
});
Or this:
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
}));
The page stays on loading and nothing happends.
Upvotes: 2
Views: 1796
Reputation: 554
So I figured it out!
The trick is on using the next(); function if captcha is correct so the code ends up like this:
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();
}
}
And thats how you integrate google captcha to your nodejs, express, passport, back-end
Upvotes: 2