不合逻辑的 Passport 验证方法参数

illogical Passport authenticate method arguments

试图理解 https://github.com/jaredhanson/passport/blob/master/lib/middleware/authenticate.js ,第 57 行。

我不明白为什么护照验证方法有 4 个参数:

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

实际是这样使用的:

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

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

那么方法定义中的第一个参数 "passport" 怎么没有干扰?由于策略名称作为第一个参数传递,因此它应该映射到护照而不是名称。

你错过了一个中间层here:

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

passport变量是Authenticatorclass的一个实例,所以上面的方法表示passport.authenticate()。如您所见,它将对自身的引用作为第一个参数传递给您所引用的函数(由 this._framework.authenticate 引用)。