API Reference

Provider

<AccessProvider>

The root provider that initializes all engines and makes access checks available to the component tree.

<AccessProvider config={config} user={user}>
  {children}
</AccessProvider>
PropTypeRequiredDescription
configAccessConfigConfig from defineAccess()
userUserContextCurrent user object
childrenReactNodeChild components

Supports nesting (inner provider shadows outer). Emits a dev warning suggesting mergeConfigs() instead.


Components

<Can>

Declarative permission/role gate. Renders children only if the check passes.

<Can permission="posts:write" fallback={<p>No access</p>}>
  <Editor />
</Can>
PropTypeDefaultDescription
perform / permissionstringSingle permission to check
permissionsstring[]Multiple permissions
rolestringSingle role check
rolesstring[]Multiple roles
on / resourceRecord<string, unknown>ABAC resource attributes
policystringPolicy evaluation
mode'all' | 'any''all'How multiple conditions are combined
childrenReactNodeContent to render if allowed
fallbackReactNodeContent to render if denied

<Feature>

Feature flag gate. Renders children only if the feature is enabled.

<Feature name="betaEditor" fallback={<ClassicEditor />}>
  <BetaEditor />
</Feature>
PropTypeDescription
namestringFeature name
childrenReactNodeRender when enabled
fallbackReactNodeRender when disabled

<AccessGate>

Multi-condition gate combining permissions, features, roles, and plans.

<AccessGate permission="billing:manage" plan="pro" feature="newBilling">
  <BillingPanel />
</AccessGate>
PropTypeDescription
permissionstringPermission check
featurestringFeature flag check
rolesstring[]Role checks
planstringPlan tier check
resourceRecord<string, unknown>ABAC resource
mode'all' | 'any'Combination mode (default: 'all')
childrenReactNodeRender when allowed
fallbackReactNodeRender when denied

<PermissionGuard>

Route-level guard that requires all specified permissions.

<PermissionGuard
  permissions={["admin:access", "settings:manage"]}
  fallback={<Unauthorized />}
>
  <AdminSettings />
</PermissionGuard>

<FeatureToggle>

Render-prop component providing feature evaluation details.

<FeatureToggle name="newDashboard">
  {({ enabled, reason }) => (
    <p>
      Feature is {enabled ? "on" : "off"} ({reason})
    </p>
  )}
</FeatureToggle>

<Experiment>

A/B experiment gate that renders the assigned variant.

<Experiment
  id="checkout-flow"
  variants={{
    control: <ClassicCheckout />,
    optimized: <NewCheckout />,
  }}
  fallback={<ClassicCheckout />}
/>

Hooks

useAccess()

Full context access — returns the complete access context.

const {
  user,
  config,
  roles,
  permissions,
  checkPermission,
  checkFeature,
  getExperiment,
} = useAccess();

usePermission(permission, resource?)

Check a single permission. Returns boolean.

const canEdit = usePermission("posts:write");
const canEditOwn = usePermission("posts:write", { ownerId: userId });

useRole()

Role checking utilities.

const { roles, hasRole, hasAnyRole, hasAllRoles } = useRole();

hasRole("admin"); // boolean
hasAnyRole(["admin", "editor"]); // boolean
hasAllRoles(["editor", "reviewer"]); // boolean

useFeature(name)

Feature flag evaluation. Returns { enabled, reason }.

const { enabled, reason } = useFeature("newDashboard");
// reason: 'static' | 'rollout' | 'role' | 'plan' | 'environment' | 'dependency' | 'disabled'

usePolicy(permission, resource?)

Policy engine evaluation. Returns { allowed, matchedRule, reason }.

const { allowed, matchedRule, reason } = usePolicy("documents:edit", {
  ownerId: "user-1",
});

useExperiment(id)

Experiment assignment. Returns { experimentId, variant, active }.

const { variant, active } = useExperiment("checkout-flow");

usePlan()

Plan/subscription utilities.

const { plan, hasPlanAccess } = usePlan();

hasPlanAccess("pro"); // true if user's plan >= 'pro'
hasPlanAccess("enterprise"); // true if user's plan >= 'enterprise'

useRemoteConfig(baseConfig, loader)

Remote config loading with stale-while-revalidate.

const { config, loading, error, stale, lastLoadedAt, refresh } =
  useRemoteConfig(baseConfig, {
    url: "/api/access-config",
    pollInterval: 60_000,
  });

useAccessDebug()

Debug information for devtools integration.

const {
  lastChecks,
  lastFeatureEvals,
  lastPolicyEvals,
  configSnapshot,
  timestamp,
} = useAccessDebug();

Utilities

defineAccess(config)

Type-safe config factory. Returns the config unchanged but provides type inference.

mergeConfigs(base, overrides)

Shallow-merges two configs. Objects are merged one level deep.

Type Inference Helpers

type Roles = InferRoles<typeof config>; // union of role strings
type Perms = InferPermissions<typeof config>; // union of permission strings
type Feats = InferFeatures<typeof config>; // union of feature names
type Plans = InferPlans<typeof config>; // union of plan strings
type Exps = InferExperiments<typeof config>; // union of experiment IDs

Use as const on arrays for literal type inference.