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 thesetupmethod.ready- Triggered when the permissions are ready to be used.hydrate- Triggered when the permissions are hydrated from the server.invalidate- Triggered when the current permission state is explicitly invalidated.
Invalidate
Use invalidate() when the current client-side permission state should no longer be trusted, for example after logout, a team switch, or a server event telling the client that roles changed:
permix.hook('invalidate', () => {
console.log('Permissions were invalidated')
})
permix.invalidate()