Skip to main content
Deprecated version. We recommend upgrading to 4.x (latest). Follow the migration guides: 2.x to 3.x · 3.x to 4.x.
Version: 2.x (deprecated)

when predicates in 2.2.3

The published TypeScript When<P> type accepts booleans, promises, sync or async predicates, and legacy Node-style callbacks.

ts
import RBAC from '@rbac/rbac';

interface Context { active: boolean; ownerId: number; userId: number }

const auth = RBAC<Context>()({
  editor: {
    can: [{
      name: 'posts:update',
      when: async ({ active, ownerId, userId }) => active && ownerId === userId
    }]
  }
});

console.log(await auth.can('editor', 'posts:update', {
  active: true, ownerId: 12, userId: 12
})); // true

Legacy callback syntax (when: (params, done) => done(null, boolean)) is also part of the 2.2.3 declared API. The 3.x migration recommends wrapping such callbacks with the explicit callbackWhen() helper for predictable behavior.

Do not copy a 3.x helper import into a 2.x project: @rbac/rbac/helpers was not declared as a package export in 2.2.3. See the migration guide.