johnbrett / hapi-auth-bearer-token

Simple Bearer authentication scheme plugin for hapi, accepts token by Header, Cookie or Query parameter.
MIT License
218 stars 46 forks source link

Required plugin #149

Closed wozuo closed 6 years ago

wozuo commented 6 years ago

Hi,

Thanks for your project I appreciate it 👍🏻 I have a problem getting it to work though, I'm trying to add authentication to an existing API Hapi project. This is my implementation of server.register:

server.register(
  [
    inert,
    vision,
    {
      register: hapiSwagger,
      options: swaggerOptions
    },
    authBearer
  ],
  function (error) {
    server.auth.strategy('simple', 'bearer-access-token', {
      allowQueryToken: true,
      validate: async (request, token, h) => {

        // TODO: Validate token
        const isValid = token === '1234';
        const credentials = { token };
        const artifacts = { test: 'info' };
        return { isValid, credentials, artifacts };
      }
    });
    server.auth.default('simple');
  }
);

And this is the error I'm getting:

if (plugin.register.register) {                             // Required plugin
                            ^

TypeError: Cannot read property 'register' of undefined
    at module.exports.internals.Server.internals.Plugin.register

I hope you can help me out. Cheers

wozuo commented 6 years ago

Ah the problem was I'm not using hapi 17 yet. Tried it with version 5.1.0 and it works 👍🏻 Thanks

johnbrett commented 6 years ago

@wozuo I was about to suggest it was the version mismatch. Glad you got it sorted! Do you think there's anything I can do to make this clearer to new users btw?

wozuo commented 6 years ago

@johnbrett Yea I think including the information in the readme is a good idea (I see you already did that 👍🏻), I didn't read the v6 release notes at first.

johnbrett commented 6 years ago

Yeah, it was after you opened this issue and I glanced at the README I released I never read the release notes when first using a module either! Thanks for the feedback!