Skip to content

Guard

Same behavior as Can: conditional render from role, permission, or permissions + match.

When to use

  • Route-level or “page shell” checks where the name Guard reads better than Can.

Signature

tsx
import { Guard } from 'advanced-react-role-guard';

<Guard role={role} children={...} />
<Guard permission={permission} fallback={...} children={...} />
<Guard permissions={permissions} match="any" | "all" children={...} />

Props

PropTypeDefaultDescription
rolestringRequire this role.
permissionstringRequire this permission.
permissionsstring[]Require a set of permissions.
match'any' | 'all''any'When using permissions, require any one or all.
fallbackReactNodenullRendered when access is denied.
childrenReactNoderequiredRendered when access is granted.

Behavior

  • Implementation matches Can line-for-line: both call useCan with the same props.

Edge cases

  • Same as Can — see Can.

Error / fallback

Renders fallback when access is denied; does not throw.

Examples

Simple (route-style)

tsx
<Guard role="admin" fallback={<p>Forbidden</p>}>
  <AdminHome />
</Guard>

Advanced (React Router redirect)

tsx
import { Navigate } from 'react-router-dom';

<Guard
  permission="billing:export"
  fallback={<Navigate to="/403" replace />}
>
  <ExportPage />
</Guard>

Do / don’t

DoDon’t
Pair with Navigate for SPA redirectsForget API-side checks

Pitfalls

  • Naming only — If your team prefers one component, using Can everywhere is fine.

Released under the MIT License.