I need to be able to add the following button to Swagger’s UI interface so that the testers can add the “Bearer token” header and test the apis.
My swagger’s option definition is:
module.exports = { definition: { openapi: "3.0.3", info: { title: "APIs", version: "1.0.0", }, servers: [ { url: `http://localhost:${process.env.PORT}` } ], securityDefinitions: { bearerAuth: { type: 'apiKey', name: 'Authorization', scheme: 'bearer', in: 'header', }, } }, apis: ["./routes/*.js", "app.js"], };
and my endpoint is as follows:
/** * @swagger * /api/users/test: * post: * security: * - Bearer: [] * summary: test authorization * tags: [User] * description: use to test authorization JWT * responses: * '200': * description: success * '500': * description: Internal server error */ router.post('/test', verifyJWT(), async (req, res) => { res.send('hi'); })
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Are you using OAS v3? You have errors in your declarations, for example securityDefinitions
is now called securitySchemes
and it is inside components
.
Check https://swagger.io/docs/specification/authentication/
When you fix your schema, then you add a security
property to your path to protect it with a security schema so that you’ll get the green Authorize
button.
components: securitySchemes: BearerAuth: type: http scheme: bearer
paths: /api/users/test: post: security: - BearerAuth: []
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0