-
Notifications
You must be signed in to change notification settings - Fork 6
Guard API
Romain Lanz edited this page Aug 12, 2017
·
5 revisions
Guard is the guardian of all your gates. It's the Facade you will use in all of your code.
Let's begin by getting the Guard facade from node-fence.
const { Guard } = require('@slynova/fence')--
Guard.setDefaultUser()
Guard allows to use a short syntax to check permission of the user. This syntax can be only used if you have already define the user you want to check. It is recommended to set the default user as the authenticated one.
Guard.setDefaultUser({ id: 1, username: 'romainlanz' })--
Guard.can()
If you don't want to use the short syntax or if you want to specify the user to check you can use the can method that will chain.
await Guard.can(user).goThroughGate('XXX').for(resource) // true or false--
Guard.allows() & Guard.denies()
If you have set the default user you are able to use the short syntax.
await Guard.allows('XXX', resource)
// or
await Guard.denies('XXX', resource)There's two methods to let you write fluent code. If allows is true, denies is false.