Type-safe role-based access control (RBAC) for React with declarative components and hooks.
- π Type-safe β Full TypeScript support with autocomplete for roles and permissions
- π― Declarative β
<Can>component for conditional rendering - πͺ Hooks β
usePermissionshook for programmatic access - π Role inheritance β Roles can inherit permissions from other roles
- π Wildcards β Support for
*(all) andresource:*(all actions) patterns - π¦ Lightweight β Zero dependencies, tiny bundle size
pnpm add react-rbac-permissions// permissions.ts
import { createPermissions } from 'react-rbac-permissions'
export const {
PermissionProvider,
usePermissions,
Can,
} = createPermissions({
roles: ['admin', 'editor', 'viewer'],
resources: {
posts: ['create', 'read', 'update', 'delete'],
users: ['read', 'invite', 'ban'],
},
permissions: {
admin: {
can: ['*'], // all permissions
},
editor: {
can: ['posts:*'], // all post actions
inherits: ['viewer'],
},
viewer: {
can: ['posts:read', 'users:read'],
},
},
})// App.tsx
import { PermissionProvider } from './permissions'
function App() {
const userRoles = ['editor'] // from your auth system
return (
<PermissionProvider roles={userRoles}>
<Dashboard />
</PermissionProvider>
)
}import { Can, usePermissions } from './permissions'
function Dashboard() {
const { can, hasRole } = usePermissions()
return (
<div>
{/* Declarative with <Can> component */}
<Can do="posts:create">
<button>Create Post</button>
</Can>
{/* With fallback */}
<Can do="users:ban" fallback={<p>No access</p>}>
<button>Ban User</button>
</Can>
{/* Multiple permissions (all required by default) */}
<Can do={['posts:update', 'posts:delete']}>
<button>Edit Post</button>
</Can>
{/* Any permission matches */}
<Can do={['posts:update', 'posts:delete']} mode="any">
<button>Manage Post</button>
</Can>
{/* Programmatic with hook */}
{can('posts:read') && <PostList />}
{hasRole('admin') && <AdminPanel />}
</div>
)
}Factory function that creates type-safe permission utilities.
| Property | Type | Description |
|---|---|---|
roles |
string[] |
List of role names |
resources |
Record<string, string[]> |
Resources and their actions |
permissions |
Record<role, { can, inherits? }> |
Permission mappings per role |
| Export | Description |
|---|---|
PermissionProvider |
React context provider |
usePermissions |
Hook for accessing permission utilities |
Can |
Declarative component for conditional rendering |
allRoles |
Set of all defined roles |
allPermissions |
Set of all possible permissions |
getPermissionsForRole(role) |
Get all permissions for a role |
getRolesWithPermission(permission) |
Get all roles that have a permission |
| Prop | Type | Default | Description |
|---|---|---|---|
do |
Permission | Permission[] |
required | Permission(s) to check |
mode |
'all' | 'any' |
'all' |
Require all or any permission |
fallback |
ReactNode |
null |
Rendered when access denied |
children |
ReactNode |
required | Rendered when access granted |
Returns an object with:
| Method | Description |
|---|---|
can(permission) |
Check single permission |
canAll(permissions) |
Check all permissions |
canAny(permissions) |
Check any permission |
hasRole(role) |
Check if user has role |
hasAllRoles(roles) |
Check all roles |
hasAnyRole(roles) |
Check any role |
roles |
Current user's roles |
permissions |
Set of resolved permissions |
getPermissionsForRole(role) |
Get permissions for a role |
getRolesWithPermission(permission) |
Get roles with a permission |
permissions: {
admin: {
can: ['*'], // All permissions
},
editor: {
can: ['posts:*'], // All actions on posts resource
},
}Roles can inherit from other roles:
permissions: {
admin: {
can: ['users:*'],
inherits: ['editor'], // Gets all editor permissions too
},
editor: {
can: ['posts:*'],
inherits: ['viewer'],
},
viewer: {
can: ['posts:read'],
},
}- Node.js 18+
- pnpm
# Install dependencies
pnpm install
# Run playground
pnpm play
# Run tests
pnpm test
# Run tests in watch mode
pnpm test --watch
# Type check
pnpm typecheck
# Build
pnpm buildβββ src/
β βββ index.ts # Entry point
β βββ create-permissions.tsx # Main implementation
β βββ create-permissions.test.tsx # Tests
βββ playground/ # Development playground
β βββ src/
β βββ App.tsx
β βββ config/permissions.ts
βββ dist/ # Built output