Overview
ThePermissions type provides a declarative way to configure role-based access control for invitation operations. It can be used as an alternative to function-based permission checks.
Type Definition
Fields
A descriptive statement identifying the permission being checked. This is typically formatted as
resource:action:operation but can be any string that helps identify the permission.Examples:"user:invite:create""user:invite:cancel""user:invite:accept""user:invite:reject"
Array of role names that have permission to perform the action. Users with any of these roles will be granted access.Examples:
["admin"]- Only admins["admin", "manager"]- Admins and managers["member", "editor", "admin"]- All authenticated users with these roles
Usage
ThePermissions type can be used with any of the can* options in InviteOptions:
canCreateInvitecanAcceptInvitecanCancelInvitecanRejectInvite
Basic Permission Configuration
Multiple Roles
All Permission Options
How It Works
When aPermissions object is used:
- The plugin extracts the user’s role from the session
- Checks if the user’s role is in the
permissionsarray - Grants or denies access based on the match
Function vs Permissions Object
You can use either a function or aPermissions object:
Using Function (More Flexible)
- Full control over logic
- Access to all context data
- Can implement complex conditions
- More verbose
- Requires manual implementation
Using Permissions Object (Simpler)
- Clean, declarative syntax
- Easy to read and maintain
- Consistent across different operations
- Less flexible
- Only supports role-based checks
- Cannot access context data
Examples
Hierarchical Permissions
Team-based Permissions
Mixed Approach
Error Handling
When permission is denied, the endpoint returns:Integration with Better Auth Roles
This works seamlessly with Better Auth’s role system. Make sure you have the role plugin configured:Source Code Reference
Type definition:src/types.ts:358-361
Permission checking: src/utils.ts (in checkPermissions function)
Related
- InviteOptions - Complete plugin configuration
- invite() - Main plugin function
- Better Auth Role Plugin - For role management