node.js 护照身份验证回调未通过 req 和 res

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21855650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:52:21  来源:igfitidea点击:

Passport authenticate callback is not passed req and res

node.jsauthenticationexpresspassport.js

提问by akaphenom

this authenticate works fine and I get a redirect:

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

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

this authenticate hangs after the call back is called:

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

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

this logs the following piece:

这记录了以下部分:

{ '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 }

But throughout the documentation (http://passportjs.org/guide/authenticate/) it looks as though it shuold be passed req and res, but it obviously isn't. Then the code that calls the callback:

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

node_modules\passport\lib\middleware\authenticate.js

node_modules\passport\lib\middleware\authenticate.js

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

doesn't pass those parameters. What am I doing wrong?

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

回答by akaphenom

OK, I was working on ripping out my custom authentication and replacing it with passport for 9 hours yesterday. Between getting the node-orm to expos the model outside of the request and dealing with the flow of ordering things I was a little burned out. THe code examples are accurate, I just needed to read more carefully:

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

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

  // generate the authenticate method and pass the req/res
  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);

});

回答by Rori Stumpf

Enable passReqToCallback to get the request in callback. Like this:

启用 passReqToCallback 以获取回调中的请求。像这样:

passport.use(new FacebookStrategy({
    clientID: '555555555555555',
    clientSecret: '555555555555555555555',
    callbackURL: "http://localhost:3000/auth/facebook/callback",
    enableProof: false,
    passReqToCallback: true
  },
  // The request will be provided as 1st param
  function(req, accessToken, refreshToken, profile, done) {
  });...