scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.42k stars 225 forks source link

Can I put Swagger UI endpoint behind express authentication middleware? #252

Closed ghettosamson closed 2 years ago

ghettosamson commented 3 years ago

I have a single page web application using Express and NodeJS on the back-end and I'm using this swagger-ui-express npm module. Our project wants to put the api-docs endpoint behind custom written authentication middleware, so you can only access the api-docs route if you are logged in. Currently this is how we add the swagger endpoint router.use('/api-docs', swaggerUi.serve, swaggerUi.setup(null, options)); Is it possible to protect this route and make it use the same authentication middleware that the rest of our routes are using, or is this not possible?

schealex commented 3 years ago

would also like to know this!

scottie1984 commented 2 years ago

I am not sure how you would get this to work with an SPA. Most likely you would want a separate URL to host the Docs directly from the API.

You should be able to add middleware though for custom authentication but it would have to respect the page coming from the server.