Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Created June 26, 2015 17:43
Show Gist options
  • Save mikermcneil/af307979566c84922c3c to your computer and use it in GitHub Desktop.
Save mikermcneil/af307979566c84922c3c to your computer and use it in GitHub Desktop.
example of using a hook to apply policies to specific shadow routes that were injected by another hook (specifically the swagger hook, in this case)
// For info on setting up this hook, check out the docs here: http://sailsjs.org/#!/documentation/concepts/extending-sails/Hooks/usinghooks.html
module.exports = function (sails) {
return {
routes: {
before: {
// If you want to protect the home route as it is exposed by the swagger hook (i.e. rather than protecting a particular action)
// you can do so by setting a shadow route here. Note however that this hook must run BEFORE the swagger hook does.
// This order-of-operations is tough to enforce without making a change to the Swagger hook to make it run later.
// If necessary, you can manually enforce hook load order by setting the `loadHooks` option in the `.sailsrc` file, or when
// Sails is loaded programmatically.
'get /': function (req, res, next) {
// Assuming you have a policy named "testpolicy" (`api/policies/testpolicy.js`), then this will work.
return sails.hooks.policies.testpolicy(req, res, next);
}
}
}
};
};
@nwwells
Copy link

nwwells commented Dec 14, 2016

as of the current version (sails 0.12.10) this doesn't work.

@fishfitz
Copy link

fishfitz commented Oct 6, 2017

Should probably be in sails.hooks.policies.middlewares.testpolicy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment