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

Is there a way to disable hooks on specific route? #487

Closed
yamalight opened this issue Nov 24, 2017 · 7 comments
Closed

Is there a way to disable hooks on specific route? #487

yamalight opened this issue Nov 24, 2017 · 7 comments
Labels
question General question about the project

Comments

@yamalight
Copy link
Contributor

I'm trying to setup auth as per example from fastify-auth:

fastify.addHook('preHandler', fastify.auth([fastify.verifyJWT]))

fastify.route({
  method: 'POST',
  url: '/auth-multiple',
  handler: (req, reply) => {
    req.log.info('Auth route')
    reply.send({ hello: 'world' })
  }
})

But I need to have a couple of routes that shouldn't trigger the auth preHandler on them.
Is there a way to disable this preHook for specific routes? Or maybe there's a different way of defining routes so that hook won't get executed?

@jsumners
Copy link
Member

Just register those routes in a different context:

fastify.register((instance, opts, next) => {
  // authenticated routes
  instance.addHook('preHandler', fastify.auth([fastify.verifyJWT])
  fastify.route(/*...*/)
})

fastify.register((instance, opts, next) => {
  // non-authenticated routes
  fastify.route(/*...*/)
})

@jsumners jsumners added the question General question about the project label Nov 24, 2017
@yamalight
Copy link
Contributor Author

@jsumners thanks!
would you be open to a docs PR that clarifies that in hooks section? currently that's only mentioned way below closer to the end, I think it'd be useful to mention the fact that hooks are scoped in the very beginning.

@jsumners
Copy link
Member

We are always receptive to PRs that improve documentation.

@yamalight
Copy link
Contributor Author

@jsumners PR open: #488
Does that looks OK?

@yamalight
Copy link
Contributor Author

Closing this.

@ahsandev2019
Copy link

i know this is old issue but i get same problem too, i solve with this

fastify.addHook("preHandler", async (request, reply) => {
  // some validation 
  return;
});

fastify.get(
  "/getaddress",
  {
    schema: {
      query: Joi.object()
        .keys({
          data1: Joi.string().required(),
        })
        .required()
    },
    validatorCompiler: ({ schema, params, method, url, httpPart }) => {
      return data => schema.validate(data);
    },
    preHandler: async (request, reply, done) => {
      // spesific validation
      return;
    }
  },

@JaosnHsieh
Copy link

i know this is old issue but i get same problem too, i solve with this

fastify.addHook("preHandler", async (request, reply) => {
  // some validation 
  return;
});

fastify.get(
  "/getaddress",
  {
    schema: {
      query: Joi.object()
        .keys({
          data1: Joi.string().required(),
        })
        .required()
    },
    validatorCompiler: ({ schema, params, method, url, httpPart }) => {
      return data => schema.validate(data);
    },
    preHandler: async (request, reply, done) => {
      // spesific validation
      return;
    }
  },

this will invoke multiple hook functions instead skipping it.

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

No branches or pull requests

4 participants