【问题标题】:illogical Passport authenticate method arguments不合逻辑的 Passport 验证方法参数
【发布时间】:2016-09-28 22:48:59
【问题描述】:

试图理解 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”没有干扰?由于策略名称作为第一个参数传递,因此它应该映射到护照而不是名称。

【问题讨论】:

  • 如果这是 Python,我会告诉你第一个参数从表达式 passport.authenticate 接收对象 passport。 Javascript 通常不会以这种方式工作,但如果有人发现有人找到了一种方法来使它以这种方式工作,我不会感到惊讶。

标签: javascript node.js express passport.js


【解决方案1】:

你错过了一个中间层here

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多