试图了解
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));
那么,方法définition中的第一个参数“ passport”如何不会产生干扰?由于策略名称是作为第一个参数传递的,因此应将其映射到通行证而不是名称。
最佳答案
您错过了中间层here:
Authenticator.prototype.authenticate = function(strategy, options, callback) {
return this._framework.authenticate(this, strategy, options, callback);
};
passport
变量是Authenticator
类的实例,因此上述方法表示passport.authenticate()
。如您所见,它将对自身的引用作为您要引用的函数(由this._framework.authenticate
引用)的第一个参数传递。