Lev Lev - 1 year ago 76
Node.js Question

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' });


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.

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download