2016-09-28 63 views
0

试图了解 https://github.com/jaredhanson/passport/blob/master/lib/middleware/authenticate.js,在第57行不合逻辑的Passport身份验证方法的参数

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

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

在实践中它的使用是这样的:

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

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

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

+0

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

回答

3

你错过的中间层here

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

passport变量是Authenticator类的一个实例,因此上述方法代表passport.authenticate()。正如你所看到的,它传递了一个对自身的引用,作为你所指的函数的第一个参数(它被this._framework.authenticate所引用)。

相关问题