最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - middleware for authorization to perform certain actions - Stack Overflow

programmeradmin1浏览0评论

I have implemented an API that is protected by JWT authorization layer. So on each endpoint before calling it I check that the user has a valid token before proceeding. It works flawlessly.

Now I want to add a new intermediate step for some endpoints.

  1. On the frontend the user hits a button to call one of these endpoints.
  2. the user is requested to insert his/her password just to confirm "are you sure you want to do this?"
  3. password is validated on the backend
  4. if password matches then the action requested is performed

My routes are defined this way

router.post('/updatePrjAttivo', verifyToken, updatePrjAttivo);

where verifyToken is the middleware for authentication. Question is: can I add a second middleware to this? Like:

router.post('/updatePrjAttivo', verifyToken, otherMiddleware, updatePrjAttivo);

where otherMiddleware is where I verify the password?

Reading answers to this question I understand that "next() : move control to next function in same route. case of multiple functions in single route." So i can chain as many functions as I want. Am I correct?

I have implemented an API that is protected by JWT authorization layer. So on each endpoint before calling it I check that the user has a valid token before proceeding. It works flawlessly.

Now I want to add a new intermediate step for some endpoints.

  1. On the frontend the user hits a button to call one of these endpoints.
  2. the user is requested to insert his/her password just to confirm "are you sure you want to do this?"
  3. password is validated on the backend
  4. if password matches then the action requested is performed

My routes are defined this way

router.post('/updatePrjAttivo', verifyToken, updatePrjAttivo);

where verifyToken is the middleware for authentication. Question is: can I add a second middleware to this? Like:

router.post('/updatePrjAttivo', verifyToken, otherMiddleware, updatePrjAttivo);

where otherMiddleware is where I verify the password?

Reading answers to this question I understand that "next() : move control to next function in same route. case of multiple functions in single route." So i can chain as many functions as I want. Am I correct?

Share edited Feb 10 at 11:57 Mureinik 312k54 gold badges358 silver badges391 bronze badges asked Feb 10 at 11:42 Lelio FaietaLelio Faieta 6,6849 gold badges47 silver badges84 bronze badges 2
  • 1 Yes, you're correct. You can have as many middlewares as you want as long as they can move on to the next one. As you've mentioned you achieve it by using next() function. – Korovjov Commented Feb 10 at 11:47
  • This question is similar to: Chaining multiple pieces of middleware for specific route in ExpressJS. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – jonrsharpe Commented Feb 10 at 11:49
Add a comment  | 

1 Answer 1

Reset to default 2

In short - yes, you can have as many middleware as you want handling a route. They are executed by order, and each middleware is responsible for calling the following one with next. If it doesn't call next (e.g., since the authentication or authorization failed), the chain is broken, and the following middleware is not called.

发布评论

评论列表(0)

  1. 暂无评论