Combine JWT middleware with other middleware
I have a file made
/middleware/jwt.ts
:
Now I'm trying to make a different middleware that checks if the user has certain roles but for this I require the jwtPayload provided by the above middleware
now I can do the following:
but it's kind of obvious that if you need to use requireRoles, jwt is also used so I'd like to use the jwt middleware inside the requireRoles middleware. Is this not possible or how should I do this? Because with everything I try I can't seem to make it work 🤔3 Replies
Usually you can spread an array of middleware to "extend" middleware. But since yours requires parameters this should work as well.
I didn't test this, it's written in Discord so forgive any mistakes
No worries, I've adjusted your code so that it works but then I'm getting
handler is not a function
, I'm really not sure why things aren't working
Update, I did find a solution that does work, not exactly what I was looking for though, what I did now is the following:
and then I can use the following for my route:
I had a feeling you might have to spread them directly
I wasn’t sure if spreading them on the return would work
Generally when I merge too I spread them on the route