From f403e214a94b672597b66ae182f8a0a9f859ec25 Mon Sep 17 00:00:00 2001 From: Joey Baker Date: Mon, 19 Sep 2016 21:47:12 -0700 Subject: [PATCH] Docs show how to interact with hapi hooks Fixes #3 --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 81b013f..bff8327 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,15 @@ await register({ // `config` if provided, overrides the default config {model: 'bat', methods: ['list'], config: { ... }}, {model: 'bat', methods: ['create']} - // change the response data {model: 'fly', config: { + // interact with the request before hapi-sequelize-crud does + , ext: { + onPreHandler: (request, reply) => { + if (request.auth.hasAccessToFly) reply.continue() + else reply(Boom.unauthorized()) + } + } + // change the response data response: { schema: {id: joi.string()}, modify: true @@ -117,6 +124,9 @@ If you want to get multiple related models, just pass multiple `include` paramet Team.findAll({include: [City, Uniform]}) ``` +## Authorization and other hooks +You can use Hapi's [`ext` option](http://hapijs.com/api#route-options) to interact with the request both before and after this module does. This is useful if you want to enforce authorization, or modify the request before or after this module does. Hapi [has a full list of hooks](http://hapijs.com/api#request-lifecycle) you can use. + ## Modify the response format By default, `hapi-sequelize-crud` routes will respond with the full model. You can modify this using the built-in [hapi settings](http://hapijs.com/tutorials/validation#output).