-
Notifications
You must be signed in to change notification settings - Fork 585
How to add middleware before specific route controller (need code example) #421
Comments
+1 |
What we did for our jwt token based out is first define securityDefinitions for the routes that should be authenticated (in the swagger definition - in your case that's getUserList) and then add swaggerSecurityHandlers (also see here) to our SwaggerExpress config. The handler itself then calls Passport or whatever middleware to then attach the user object to the request so it can be used in the following controller. Does that sound like sth. that would work for you? |
Hi frederik, it sounds like something that would sure work for me. I've already wasted two days looking for simple answer how to use passport and swagger. I once had working app with passport but without proper restful api neither swagger. Now I'm refactoring. |
Hi Patryk, (in hope this is still useful to someone) whether you need JWT really depends on your use case (maybe have a look here). With JWT it'd look sth. like this. First, you need to add the handler to the swagger config like this.
To configure passport you can register it with express like this:
Then define the authenticateJwt where it has access to the configured Passport
|
Hi Frederik, Great job. Worked for me. Thank you! |
I ended up not using passport, neither swagger as framework (just documenting my routes set up in traditional way). Anyway, thanks for your answer :) |
I have been searching everywhere and don't seem to have found anything useful. I have generated a swagger project for express and been trying to add a auth middleware to specific route. I have implemented a custom auth middleware and been trying to execute it before
getUserList
controller.How do I do that if some one could explain with an example that would be very helpful.
The text was updated successfully, but these errors were encountered: