node.js - Passport 身份验证回调未通过 req 和 res

标签 node.js authentication express passport.js

这个验证工作正常,我得到一个重定向:

server.post(authPostRoute, passport.authenticate(
    'local'
    , { successRedirect: '/', failureRedirect: '/login' }
));     

调用回调后,此身份验证挂起:

server.post(authPostRoute, passport.authenticate(
    'local'
    , function(){ console.log('Hitting the callback'); console.log(arguments)}
));     

这会记录以下内容:

{ '0': null,
  '1':
   { id: [Getter/Setter],
     firstName: [Getter/Setter],
     lastName: [Getter/Setter],
     email: [Getter/Setter],
     addedOn: [Getter/Setter],
     active: [Getter/Setter],
     password: [Getter/Setter] },
  '2': undefined }

但是在整个文档 (http://passportjs.org/guide/authenticate/) 中,它看起来好像应该传递 req 和 res,但显然不是。然后是调用回调的代码:

node_modules\passport\lib\middleware\authenticate.js

  strategy.success = function(user, info) {
    if (callback) {
      return callback(null, user, info);
    }

不传递这些参数。我做错了什么?

最佳答案

好的,我昨天花了 9 个小时努力取消我的自定义身份验证并用 Passport 替换它。在让 node-orm 在请求之外公开模型和处理订购东西的流程之间,我有点筋疲力尽。代码示例是准确的,我只需要仔细阅读:

// traditional route handler, passed req/res
server.post(authPostRoute, function(req, res, next) {

  // generate the authenticate method (the anonymous method) and
  //     associate it with the 'local' strategy
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err); }
    if (!user) { return res.redirect('/'); }

    // req / res held in closure
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.send(user);
    });

  })(req, res, next);

});

关于node.js - Passport 身份验证回调未通过 req 和 res,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855650/

相关文章:

authentication - 在使用 Volley 重试旧请求之前重试获取新 token

spring - 如何在同一应用程序中使用 spring-sample 示例配置 spring 安全基本身份验证和 SAML 身份验证

express - Mongoose :如何使用 ObjectId 链接模型?

javascript - 在expressJS应用程序中,是否可以在脚本执行时实时收集写入控制台的数据?

node.js - 如果没有一些内部模块,如何构建node.js?

Node.js process.domain 有时未定义

node.js - 如何正确处理子进程的句柄

javascript - 将 javascript 变量传递给 XML 主体 node.js

authentication - 使用 Laravel Auth/Hash 更改 bcrypt 成本

node.js - NodeJs : slow req. 管道