Permix

Events

Learn how to handle permission updates in your application

Overview

Permix provides an event system that allows you to react to permission changes in your application. Each event provides type-safe data and hooks to register handlers.

Usage

You can register event handlers using the hook and hookOnce methods:

const permix = createPermix<{
  post: {
    action: 'create' | 'read'
  }
}>()
 
// The handler will be called every time setup is executed
permix.hook('setup', () => {
  console.log('Permissions were updated')
})
 
// The handler will be called only once
permix.hookOnce('setup', () => {
  console.log('Permissions were updated once')
})
 
// Calling `setup` method triggers the `setup` event
// and `ready` event if called on the client side
permix.setup({
  post: {
    create: true,
    read: true
  }
})

Available events:

  • setup - Triggered when permissions are updated through the setup method.
  • ready - Triggered when the permissions are ready to be used.
  • hydrate - Triggered when the permissions are hydrated from the server.

On this page