Installation — @piplup/react-acl
Install @piplup/react-acl and show or hide React UI by the current user’s roles and permissions, with wildcards and typed permission names.
@piplup/react-acl holds the current user — the principal — in a context and gates UI on their
roles and permissions. It gives you a Can component for markup, a useAccessControl hook for
checks in code, and the same checks as plain functions for everything outside React.
npm install @piplup/react-aclQuickstart
Wrap your app in AccessControlProvider and pass the principal. The provider fetches nothing — load
it however you like and pass the result in.
import { AccessControlProvider, Can } from '@piplup/react-acl';
export default function App({ user }) {
return (
<AccessControlProvider principal={user}>
<Can permission="invoice:write" fallback={<ReadOnlyNotice />}>
<EditInvoice />
</Can>
</AccessControlProvider>
);
}A principal is just roles and permissions:
{ roles: ["admin"], permissions: ["invoice:read", "invoice:write"] }Pass undefined while you are still fetching it. Every check then returns false and Can renders
its loading slot instead of its fallback, so nobody sees an access-denied state flash before the
data arrives. See AccessControlProvider.
This hides UI. It does not protect data. Anything a user must not reach needs a check on your server as well.
Upgrading from 3.x
Version 4 replaced the whole API: AclProvider, HasAccess and useAcl are gone, roles and
permissions moved into a single principal, permission lists now default to requiring all of
them rather than any, and permissions gained wildcards. The mapping is in the
package's migration guide.
See also
- Role-based access control in React — a step-by-step guide
- Permissions — the permission model, wildcards and typed names
- AccessControlProvider
- Can
- useAccessControl
- Check functions
react-acl vs CASL
Compare CASL and @piplup/react-acl for React permissions: permission strings versus conditional rules, loading states, typing, server checks and when to use each.
Permissions
How permission names are matched, how wildcard grants work, and how to narrow Permission and Role to your own names.