Lev
Lev

Reputation: 15684

illogical Passport authenticate method arguments

Trying to understand https://github.com/jaredhanson/passport/blob/master/lib/middleware/authenticate.js , at line 57.

I dont understand why the passport authenticate method has 4 arguments :

module.exports = function authenticate(passport, name, options, callback){/*code*/}

In practice it's used like this:

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

or

passport.authenticate('local', function(req, res));

So how come the first argument "passport" in the method définition is not interfering ? Since the strategy name is passed as first argument, it should be mapped to passport not to name.

Upvotes: 0

Views: 142

Answers (1)

robertklep
robertklep

Reputation: 203286

You missed an intermediate layer here:

Authenticator.prototype.authenticate = function(strategy, options, callback) {
  return this._framework.authenticate(this, strategy, options, callback);
};

The passport variable is an instance of the Authenticator class, so the method above represents passport.authenticate(). As you can see, it passed a reference to itself as first argument to the function that you're referring to (which is referred to by this._framework.authenticate).

Upvotes: 3

Related Questions