node.js - Validating captcha along with passport.js -


is possible verify if google recaptcha succeeded before calling passport.js authenticate function?

i'm getting stuck between choosing 1 or other because both use asynchronous callbacks verify , can't next them within each other.

    function verifyrecaptcha(key, rq, rs, pa, callback) {     https.get('https://www.google.com/recaptcha/api/siteverify?secret=' + secret + '&response=' + key, function (res) {         var data = '';         res.on('data', function (chunk) {             data += chunk.tostring();         });         res.on('end', function () {             try {                 var parseddata = json.parse(data); //                    return true;                 callback(parseddata.success, rq, rs, pa);             } catch (e) { //                    return false;                 callback(false, rq, rs, pa);             }         });     }); }      app.post('/auth/signin', can_access.if_not_logged_in(), passport.setuplocalstrategy(),     function (req, res) {         console.log('here');          verifyrecaptcha(req.body['g-recaptcha-response'], function (success) {             if (success) {                 // find way signal captcha succeeded.                 return true;             } else {                 res.end('captcha failed, sorry.');                 // todo: take them previous page                 // , love of everyone, restore inputs                 return false;             }         });     },     passport.authenticate('local', {         successredirect: '/',         failureredirect: 'auth/signin',         failureflash: true     })); 

i want authentication after captcha has succeeded

the way express middlewares or route handlers work executed in succession, 1 after another, long previous 1 called next()

so need call next() captcha verifying middleware passport.authenticate middleware comes next can executed.

also, if call next(err) (i.e. passing error) skip middlewares , go straight next middleware has 4 argument signature (err, req, res, next), main error handler placed @ or near end in routes/middlewares.

so try changing code this:

app.post('/auth/signin', can_access.if_not_logged_in(), passport.setuplocalstrategy(), function (req, res, next) { // <<-- 1. have next passed here     console.log('here');      verifyrecaptcha(req.body['g-recaptcha-response'], function (success) {         if (success) {             // find way signal captcha succeeded.             return next(); // <<-- 2. call next();          } else {             res.end('captcha failed, sorry.');             // todo: take them previous page             // , love of everyone, restore inputs             return false;         }     }); }, // invoked if next() called previous middleware  passport.authenticate('local', {     successredirect: '/',     failureredirect: 'auth/signin',     failureflash: true })); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -