Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WillDent/074a88d4f0a0fdfecae7f36b3df6fe03 to your computer and use it in GitHub Desktop.
Save WillDent/074a88d4f0a0fdfecae7f36b3df6fe03 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);
}
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment