2016-09-28 64 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所引用)。

相關問題