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

illogical Passport authenticate method arguments

本文关键字:方法 参数 验证 Passport 不合逻辑      更新时间:2023-09-26

试着理解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));

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

您这里漏了一个中间层:

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

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