Created
June 26, 2015 17:43
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
Should probably be in
sails.hooks.policies.middlewares.testpolicy
.