oRPC
Learn how to use Permix with oRPC
Overview
Permix provides a middleware for oRPC that allows you to easily check permissions in your middlewares. The middleware can be created using the createPermix
function.
Before getting started with oRPC 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 oRPC:
The middleware preserves the context and input types from your oRPC middlewares, ensuring end-to-end type safety in your API.
Accessing Permix in Middlewares
Permix is automatically added to your oRPC context, so you can access it directly:
The context.permix
object contains two methods:
check
: Synchronously check a permissioncheckAsync
: Asynchronously check a permission
Custom Error Handling
By default, the middleware throws an ORPCError
with code FORBIDDEN
. You can customize this behavior by providing a forbiddenError
option:
Static Error
Dynamic Error
You can also provide a function that returns different errors based on the entity and actions:
The forbiddenError
handler receives:
context
: Your oRPC 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:
Type Safety
oRPC with Permix provides full type safety for your context and input parameters: