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.
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?
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.