What it does
The permission system provides a complete authorization layer for React applications. At its core is the PermissionProvider context that stores the current user's permissions and roles. From there, you can use declarative components like Can, Cannot, and Role to conditionally render UI, hooks like usePermission and useCan for programmatic checks, and RouteGuard for route-level protection.
The system supports role-based access control (RBAC), permission-based access control (PBAC), wildcards, async permission loading, and both "all" and "any" matching modes.
Common use cases
- Protect UI elements like buttons, links, and menu items based on user permissions.
- Guard entire route segments with RouteGuard and redirect unauthorized users.
- Show or hide page sections based on roles using the Role component.
- Provide loading and fallback states while permissions are being fetched asynchronously.
- Build admin panels where only users with specific roles can access management features.
API reference
Import everything from @zentauri-ui/zentauri-components/permission.
Components: PermissionProvider (context provider), Can (render if authorized), Cannot (inverse of Can), Role (render if role matches), RouteGuard (route-level protection with redirect), PermissionBoundary (empty-permission fallback boundary).
Hooks: usePermission (check single permission), usePermissions (get all permissions), usePermissionsRefresh (get refresh function), useRole (check role), useRoles (get all roles), useCan (detailed multi-permission check with missing list), usePermissionContext (raw context access).
Utilities: hasPermission, hasAnyPermission, hasAllPermissions, hasRole, mergePermissions, getMissingPermissions, matchWildcard, hasWildcard.
Next.js integration notes
Keep PermissionProvider in a client component wrapper around your layout or app. Use RouteGuard for route-level protection. Components like Can and Cannot run in client components and respect the provider context.
FAQ
Does the Permission System work with Next.js App Router?
Yes. Wrap your layout or app with PermissionProvider inside a client component boundary. RouteGuard performs client-side gating and redirect — it cannot protect server components or backend resources. For protected routes and data, combine RouteGuard with server-side authorization checks in your API routes or server components.
How do I load permissions from an API?
Use the loadPermissions prop on PermissionProvider with an async function. The provider will show the fallback UI while permissions are loading.
Can I use wildcards in permissions?
Yes. The permission system supports wildcards like users.*, billing.*, and *. This allows you to define broad permission sets and check granular permissions.
What's the difference between Can and RouteGuard?
Can conditionally renders its children based on permissions or roles. RouteGuard additionally supports a redirectTo prop that navigates away when unauthorized, making it suitable for route-level protection.