Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport authenticate callback is not passed req and res

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:

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?

like image 907
akaphenom Avatar asked Feb 18 '14 13:02

akaphenom


Video Answer


2 Answers

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

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) {
  });...
like image 23
Rori Stumpf Avatar answered Sep 18 '22 12:09

Rori Stumpf


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:

// 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);

});
like image 104
akaphenom Avatar answered Sep 18 '22 12:09

akaphenom