我继承了一个代码库,它看起来像是在节点中运行中间件,其Oauth2护照策略的模式如下
module.exports = function (router) {
router.get('/', function (req, res, next) {
passport.authenticate('type', object, function(err, info) {
//pass info object to next middleware
})(req,res,next) <---where does this go?!?
})
}根据我目前对代码库的理解,这实际上是中间件链中的最后一个函数调用,所以我可以在底层添加一个中间件吗?
这听起来像正确的主意吗?
为了澄清我想做的事:
发布于 2016-07-17 15:59:11
这似乎是使用护照的authenticate函数的“自定义回调”方法。如果您查看文献资料,您可以看到他们期望如何使用它。尽管如此,我不知道第二个参数应该做什么( object) --它看起来像一个变量,但我没有看到它在任何地方定义,而且我也不确定authenticate方法是否以这种方式接受参数。此外,自定义回调包含三个参数:err、user和info.这可能会把你绊倒。
好吧,那么现在你的实际问题是:“我可以在底部添加一个中间件吗?”说大也大吧?事实是,此时您正处于路由中间件中。如果它匹配并且auth是成功的,那么您应该在自定义回调()中执行该路由所需的任何代码。这就是这种做事方式的意义所在。或者,您可以使用passport.authenticate作为中间件本身的一部分(它返回一个在CommonJS模式中可用的中间件函数。
如果您不想更改代码,那么您可以这样做:
module.exports = function (router) {
router.get('/', function (req, res, next) {
passport.authenticate('PICK A VALID TYPE', function(err, user, info) {
// this custom callback will be executed once auth completes
// (either successfully or not
// put code in here to perform DB business logic, login, and redirect
})(req,res,next); <--- this executes the passport.authenticate middleware
})
};https://stackoverflow.com/questions/38422949
复制相似问题