Passport身份验证回调未通过req和res

时间:2014-02-18 13:41:17

标签: 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 \护照\ lib中\中间件\ authenticate.js

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

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

2 个答案:

答案 0 :(得分:17)

好的,我正在努力剥离我的自定义身份验证,并在昨天用护照替换它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);

});

答案 1 :(得分:1)

启用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) {
  });...