Hono
Learn how to use Permix with Hono
Overview
Permix provides middleware for Hono that allows you to easily check permissions in your routes. The middleware can be created using the createPermix
function.
Before getting started with Hono integration, make sure you've completed the initial setup steps in the Quick Start guide.
Setup
Here's a basic example of how to use the Permix middleware with Hono:
The middleware preserves full type safety from your Permix definition, ensuring your permission checks are type-safe.
Accessing Permix Directly
You can access the Permix instance directly in your route handlers using the get
function:
The get
function returns the Permix instance with available methods.
Custom Error Handling
By default, the middleware returns a 403 Forbidden response. You can customize this behavior by providing an onForbidden
handler:
Basic Error Handler
Dynamic Error Handler
You can also provide a handler that returns different responses based on the entity and actions:
The onForbidden
handler receives:
c
: Hono Context objectentity
: The entity that was checkedactions
: Array of actions that were checked
Advanced Usage
Async Permission Rules
You can use async functions in your permission setup:
Dynamic Data-Based Permissions
You can check permissions based on the specific data being accessed: