Skip to content

Gate API

Romain Lanz edited this page Aug 12, 2017 · 11 revisions

What's a gate & policy?

Gates are closure that returns a boolean to determine if the user is allowed to perform an action.
Policies are classes that let you organise your authorisation around a particular model or resource.

Those are defined using the Gate facade. You need to required it via node-fence.

const { Gate } = require('@slynova/fence')

Writing Gates

To define a new gate you will need to call the define method on the Gate facade.

Gate.define('nameOfTheGate', async (user, resource) => {
  // Write your payload here.
  // You should return a boolean value
  // example: return user.id === resource.author_id
})

The parameter of the callback will be send via the Guard facade.

Writing Policies

To define a new policy you will need to call the policy method on the Gate facade.

// post is an ES2015 Object
Gate.policy(post, PostPolicy)

The first argument is the object you want to define the policy for. It can be a simple JSON or an ES2015 class.

The policy is an ES2015 class.

Clone this wiki locally