简体   繁体   English

nodeJS代码模式=> Express中间件/ Oauth2 / Passport

[英]nodeJS code pattern => Express middleware/Oauth2/Passport

I inherited a codebase where it looks like they run middleware in node with the following pattern for Oauth2 passport strategy 我继承了一个代码库,看起来它们在节点中使用以下模式针对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?!?
})
}

From my current understanding of the code base, this is actually the last function call in the middleware chain, so could I just add a piece of middleware to the bottom? 从我目前对代码库的理解来看,这实际上是中间件链中的最后一个函数调用,因此我可以在中间添加一个中间件吗?

Does this sound like the right idea? 这听起来像是正确的主意吗?

And just to clarify what I'm trying to do: 只是为了澄清我要做什么:

  1. pass data from Oauth callback through middleware function by attaching it to the req 通过将Oauth回调附加到req来通过中间件函数传递数据
  2. perform DB business logic (create or lookup account) 执行数据库业务逻辑(创建或查找帐户)
  3. login with JWT 用JWT登录
  4. redirect 重新导向

This appears to be the "custom callback" method of using passport's authenticate function. 这似乎是使用护照的authenticate功能的“自定义回调”方法。 If you look at the documentation you can see how they expect it to be used. 如果您查看文档 ,则可以看到他们期望如何使用它。 That said, I don't know what that second argument is supposed to be doing (the object ) - it looks like a variable, but I don't see it defined anywhere, and I'm not sure the authenticate method takes arguments in that manner. 就是说,我不知道第二个参数应该做什么( object )-它看起来像一个变量,但是我看不到它在任何地方定义,而且我不确定authenticate方法是否接受参数这样。 Also, the custom callback takes three arguments: err , user , and then info ... which might trip you up. 此外,定制的回调有三个参数: erruser然后 info ...可能你绊倒。

Okay, so now to your actual question of "could I just add a piece of middleware to the bottom?" 好的,现在您的实际问题是:“我可以在中间添加一块中间件吗?” Sort of? 有点? The fact is, you're in a routing middleware at that point. 事实是,此时您正在使用路由中间件。 If it matches and auth is successful, then you should do whatever code for that route is required inside the custom callback. 如果匹配,并权威性是成功的,那么你应该做任何代码,这条路是需要自定义回调里面 That's the point of this way of doing things. 这就是这种做事方式的重点。 Alternatively you could use passport.authenticate as a piece of middleware itself (it returns a middleware function usable in the CommonJS pattern. 另外,您也可以将passport.authenticate本身用作中间件(它返回可在CommonJS模式中使用的中间件功能)。

If you don't want to change up the code, then you could just do this: 如果您不想更改代码,则可以这样做:

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
  })
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM