【问题标题】:Validating captcha along with passport.js验证验证码和 passport.js
【发布时间】:2015-06-23 07:12:52
【问题描述】:

是否可以在调用passport.js验证函数之前验证google recaptcha是否成功?

我在选择一个或另一个之间陷入困境,因为两者都使用异步回调进行验证,而我无法将它们放在一起。

    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 a way to signal captcha succeeded.
                return true;
            } else {
                res.end('Captcha failed, sorry.');
                // TODO: take them back to the previous page
                // and for the love of everyone, restore their inputs
                return false;
            }
        });
    },
    passport.authenticate('local', {
        successRedirect: '/',
        failureRedirect: 'auth/signin',
        failureFlash: true
    }));

我想在验证码成功后进行身份验证

【问题讨论】:

  • 您能否展示您现有的代码,因为您无法同时调用两个异步回调?
  • 我在卡住的地方添加了代码。我想首先检测验证码是否成功,如果成功,它将返回 true,这将允许 post 方法移动以检查身份验证。

标签: node.js authentication passport.js recaptcha


【解决方案1】:

Express 中间件或路由处理程序的工作方式是它们连续执行,一个接一个,只要前一个称为next()

所以你只需要从你的验证码验证中间件调用next(),这样你接下来的passport.authenticate中间件就可以执行了。

此外,如果您调用 next(err)(即传递一个错误),它将跳过所有中间件并直接转到具有 4 个参数签名 (err, req, res, next) 的下一个中间件,这通常是放置在的主要错误处理程序或在您的路线/中间件接近尾声。

所以只需尝试将您的代码更改为:

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 a way to signal captcha succeeded.
            return next(); // <<-- 2. call next(); 
        } else {
            res.end('Captcha failed, sorry.');
            // TODO: take them back to the previous page
            // and for the love of everyone, restore their inputs
            return false;
        }
    });
},
// this will only be invoked if next() was called from previous middleware 
passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: 'auth/signin',
    failureFlash: true
}));

【讨论】:

  • 非常感谢!那行得通。我不知道下一个函数是干什么用的。
猜你喜欢
  • 2015-01-09
  • 1970-01-01
  • 2012-07-29
  • 2014-11-06
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
相关资源
最近更新 更多