17

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?

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
akaphenom
  • 6,728
  • 10
  • 59
  • 109

2 Answers2

30

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

});
akaphenom
  • 6,728
  • 10
  • 59
  • 109
  • in passport.authenticate() what is err,user,info parameters? – hemal7735 Mar 10 '16 at 06:05
  • It has been a long time since I have worked with this, but I believe that when passport is configured you provide it a function which takes a UN / PWD which you can then authenticate against your source (this happens within the passport.authenticate('local',...) That implementation provides the values err and user. I am certain it provides info as well - but I cannot remember the inteded use of that field. – akaphenom Mar 10 '16 at 14:20
  • The err,user,info parameters are coming as null,false,missing credentials, can you know what is mistake. refer my question please http://stackoverflow.com/questions/42898027/passport-js-local-strategy-custom-callback-showing-user-as-false-and-info-as-mi – charan tej Mar 20 '17 at 07:50
  • @charantej do you have any idea why err, user, info parameters are not null, false and missing as normal? Err is `[{"id":1,"email":"a@gmail.com","password":"a"}]`, user and info are `undefined` – Mr. Wizard Sep 13 '18 at 12:15
  • 1
    I am new to javascript. So what is `(req, res, next);` after the `passport.authenticate()` function for? – Suri Aug 27 '20 at 06:27
  • not sure what you mean exactly, but you might serarch for "continutation passing style" and the alternative approach "promises" to help you get familiar with js. – akaphenom Aug 27 '20 at 20:14
2

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) {
  });...
Rori Stumpf
  • 1,912
  • 19
  • 26
  • 1
    This will pass it to the *strategy* callback, not to the *authenticate* callback. But since you can define your own strategy callback, which you can call the authenticate callback from, you can pass it req as well. – Adam Reis Feb 13 '19 at 21:11