admin管理员组文章数量:1302870
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.
- On the frontend the user hits a button to call one of these endpoints.
- the user is requested to insert his/her password just to confirm "are you sure you want to do this?"
- password is validated on the backend
- 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.
- On the frontend the user hits a button to call one of these endpoints.
- the user is requested to insert his/her password just to confirm "are you sure you want to do this?"
- password is validated on the backend
- 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
1 Answer
Reset to default 2In 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.
本文标签: nodejsmiddleware for authorization to perform certain actionsStack Overflow
版权声明:本文标题:node.js - middleware for authorization to perform certain actions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741718758a2394265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论