user2309803
user2309803

Reputation: 645

passportjs configuration `Done` callback

I have been studying the Passport configure documention -

var passport = require('passport')
  , LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, { message: 'Incorrect username.' });
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
  }
));

According to the docs, if the credentials are valid, the verify callback function(username, password, done){...} invokes done to supply Passport with the user that authenticated. Since done is also callback function, I would expect its function body to be passed in as a parameter of the verify callback. In the Express 4.x Passport example, the code is similar except the done is called cb. What is the benefit of being able to change its name? In both cases, I can't find the body of the callback so I assume it is part of Passport - is this correct? The documentation give examples how to call Done but I can't find its API call signature. In the first example, result calling Done is returned (for example return done(null, user);) but how do we know what Done actually returns?

Upvotes: 1

Views: 204

Answers (2)

Yuriy Vorobyov
Yuriy Vorobyov

Reputation: 775

It makes no difference what you call the callback. It's like a (req, res, next) in Express.js, you can call it: (request, response, nextFunc), but it doesn't change a function work

Upvotes: 2

Quentin
Quentin

Reputation: 943517

What is the benefit of being able to change its name?

You can name function parameters whatever you like. That's just how functions work.

Express couldn't stop you from writing your function as function(alice, bob, charline) however nonsensical that might be.

In both cases, I can't find the body of the callback so I assume it is part of Passport - is this correct? The documentation give examples how to call Done but I can't find its API call signature.

It's in the Strategy section under "Verify Callback".

In the first example, result calling Done is returned (for example return done(null, user);) but how do we know what Done actually returns?

You could log it and see, but the documentation doesn't mention what it returns, only the arguments it accepts. The use of a return statement is most likely to exit the function so no further processing is performed.

Upvotes: 2

Related Questions