1

I am running an express/node application and am documenting my api using "swagger-ui-express": "^4.5.0",. I have set up a requirement of needing a jsonwebtoken bearer token to be sent with all requests to any endpoint in my api.

I have the swagger docs loaded and working properly but now when trying to figure out how to pass the Authorization: Bearer <token> to all my endpoints, it doesn't seem to work. I am able to add the securitySchemes + child options and I get the green Authorize button in my swagger docs, but when I enter a bearer token and send off the request the loading spinner keeps spinning and never sends the request. I have morgan logging set up in my app so I can see that the request to my endpoint never gets sent or logged.

How do I send a bearer token to requests sent from swagger UI?

In app.js I have this route which loads properly in localhost

// Single entry point for swagger docs
router.use(
  '/swaggerDocs',
  swaggerDoc.serve,
  swaggerDoc.setup(swaggerDocumentation),
);

swaggerDocumentation from above snippet (config file).

import getCountryRegions from './getCountryRegions.doc.js';

export default {
  openapi: '3.0.3',
  info: {
    title: 'Node/express rest api app',
    version: '0.0.1',
  },
  components: {
    securitySchemes: {
      bearerAuth: {
        type: 'http',
        in: 'header',
        name: 'Authorization',
        description: 'Bearer Token',
        scheme: 'bearer',
        bearerFormat: 'JWT',
      },
    },
  },
  security: {
    bearerAuth: [],
  },
  servers: [
    {
      url: 'http://localhost:3010/api',
      description: 'Local server',
    },
  ],
  paths: {
    ...getCountryRegions,
  },
};

Modal to enter bearer token enter image description here

Adding token enter image description here

Request is sent but it spins endlessly without ever sending the request enter image description here

No errros in my application terminal or logging but I do see one error in the chrome browser console when sending the request: enter image description here

Helen
  • 87,344
  • 17
  • 243
  • 314
CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49
  • Any errors in console? – Anatoly Nov 23 '22 at 18:46
  • Hi @Anatoly I added more info to my question. I actually do see an error in the console that does not stem from my code directly. Seems to be an issue with something in Swagger? – CodeConnoisseur Nov 23 '22 at 18:49
  • [Export](https://stackoverflow.com/a/48525934/113116) your OpenAPI YAML/JSON file from Swagger UI and then paste its contents into https://editor.swagger.io. Does the editor flag any errors? – Helen Nov 23 '22 at 19:03

1 Answers1

5

I figured out the issue...the security property has to have an [] wrapped around it's object.

components: {
    securitySchemes: {
      bearerAuth: {
        type: 'http',
        in: 'header',
        name: 'Authorization',
        description: 'Bearer token to access these api endpoints',
        scheme: 'bearer',
        bearerFormat: 'JWT',
      },
    },
  },
  security: [
    {
      bearerAuth: [],
    },
  ],

This chunk of code works.

CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49