Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to display the authenticate button #172

Open
samylaumonier opened this issue Dec 17, 2017 · 4 comments
Open

Not able to display the authenticate button #172

samylaumonier opened this issue Dec 17, 2017 · 4 comments

Comments

@samylaumonier
Copy link

Hi @z0mt3c, I'm trying to configure hapi-swaggered but I'm not able to display the authenticate button. Do you have a working example using the authorization option? Even with your example code, the configuration seems to be useless.

const Hapi = require('hapi');
const Joi = require('joi');

const server = Hapi.Server({ port: 8000 });

(async () => {
  await server.register([
    require('inert'),
    require('vision'),
    {
      plugin: require('hapi-swaggered'),
      options: {
        tags: {
          'foobar/test': 'Example foobar description',
        },
        info: {
          title: 'Example API',
          description: 'Desc...',
          version: '1.0',
        },
      },
    },
    {
      plugin: require('hapi-swaggered-ui'),
      options: {
        title: 'Example API',
        path: '/docs',
        authorization: {
          field: 'apiKey',
          scope: 'query', // header works as well
          // valuePrefix: 'bearer', // prefix incase
          defaultValue: 'demoKey',
          placeholder: 'Enter your apiKey here',
        },
        swaggerOptions: {
          validatorUrl: null,
        },
      },
    },
  ]);

  server.route({
    path: '/',
    method: 'GET',
    async handler(request, h) {
      return await h.redirect('/docs');
    },
  });

  server.route({
    path: '/foobar/test',
    method: 'GET',
    options: {
      tags: ['api'],
      description: 'My route description',
      notes: 'My route notes',
      handler() {
        return {};
      },
    },
  });

  server.route({
    path: '/foobar/{foo}/{bar}',
    method: 'GET',
    options: {
      tags: ['api'],
      validate: {
        params: {
          foo: Joi.string()
            .required()
            .description('test'),
          bar: Joi.string().required(),
        },
      },
      handler() {
        return {};
      },
    },
  });

  try {
    await server.start();
    console.log('started on http://localhost:8000');
  } catch (err) {
    console.error(err);
  }
})();

Result:
capture d ecran 2017-12-17 a 23 40 07

Thanks,
Samy

@m1m1s1ku
Copy link

m1m1s1ku commented Apr 12, 2018

Same here :'(

See here for a workaround : #165

@samylaumonier
Copy link
Author

I switched to hapi-swagger...

@carlos-r-mendoza
Copy link
Contributor

carlos-r-mendoza commented Jan 7, 2019

I am using a combination of hapi-swagger to create the swagger definition and hapi-swaggered-ui for the ui. Of course, you can just stick with hapi-swagger for both the swagger definition and ui, but I found that hapi-swaggered-ui to be using a more modern version of swagger ui. Hence, why I combined both libraries. With that combination, you can do the following:

Add a securityDefinitions value to the options for hapi-swagger.

{
    ...options,
    // documentation for security configs: https://swagger.io/docs/specification/2-0/authentication/
    securityDefinitions: {
      'Bearer': {
        type: 'apiKey',
        name: 'Authorization',
        in: 'header'
      }
    },
    security: [{
      'Bearer': []
    }],
    documentationPage: false, // we are using `hapi-swaggered-ui` for the ui, so we turn off the ui for 
    `hapi-swagger`
}

@tamohugues
Copy link

tamohugues commented Apr 6, 2019

this not work, plz show one exemple, I go error

"securityDefinitions" is not allowed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants