Role-Based Auth
Implement fine-grained authorization with the auth-rbac package.
Role-Based Access Control (RBAC)
The @zopio/auth-rbac
package provides a flexible and powerful role-based access control system for your application. It allows you to define granular permission rules based on user roles, resources, actions, and even field-level permissions.
Overview
RBAC (Role-Based Access Control) is an approach to restricting system access to authorized users based on their role within an organization. The @zopio/auth-rbac
package extends the authentication capabilities provided by @zopio/auth
with robust authorization features.
Key features include:
- Resource and action-based permissions: Control who can perform specific actions on resources
- Field-level permissions: Define read/write access at the field level
- Conditional rules: Apply complex logic to determine access rights
- DSL support: Use a declarative syntax for defining access rules
- React hooks: Easy integration with your frontend components
- Middleware: Protect API routes with authorization checks
Installation
The package is included by default in the zopio stack. If you need to install it separately:
Configuration
Defining Rules
Authorization rules are defined in a central configuration file. Each rule specifies:
- The resource being accessed (e.g., “orders”, “users”)
- The action being performed (e.g., “read”, “update”, “delete”)
- Optional condition function that evaluates access based on user context and record data
- Optional field permissions that specify read/write access to individual fields
Using the DSL (Domain Specific Language)
For more complex access rules, you can use the DSL syntax instead of condition functions:
API Reference
Middleware
Protect your API routes with the withAuthorization
middleware:
React Hooks
Use the useAccess
hook in your components to conditionally render UI elements based on permissions:
For field-level permissions:
Integration with Clerk
The package seamlessly integrates with Clerk authentication through the @zopio/auth
package. The user context is automatically extracted from the Clerk session:
Best Practices
- Define granular permissions: Create specific rules for each resource and action combination
- Use field-level permissions: Control access to sensitive fields
- Keep rules maintainable: Group related rules and use comments to explain complex conditions
- Test thoroughly: Verify that your permission rules work as expected in all scenarios
- Consider performance: Complex rule evaluations can impact performance, so optimize where needed
Example: Complete Authorization Flow
Here’s a complete example of how authorization works in a typical zopio
application:
- User authenticates with Clerk
- User makes a request to a protected API route
- The
withAuthorization
middleware extracts the user context from the Clerk session - The middleware evaluates the permission rules against the user context and requested resource
- If authorized, the request proceeds; otherwise, a 401 or 403 response is returned
- On the frontend, components use the
useAccess
hook to conditionally render UI elements based on the user’s permissions
This flow ensures that authorization is consistently applied across both the backend and frontend of your application.