Skip to main content

Checking permissions in your own UI

Beyond the admin editor, @archenstack/rbac-web/react gives you a permission layer for your own app: decide what to show, enable, or route to based on the current user's permissions. It's the runtime counterpart to the admin editor — that one edits the matrix; this one reacts to it.

These are UI hints, not enforcement

Everything here decides what to show, never what a user may do. A hidden button, a disabled control, and a filtered route are UX only — every mutation and every data-load must re-authorize on the server. The API below is deliberately shaped to make crossing that boundary by accident hard.

One provider, one snapshot​

Wrap your tree in a single provider. It owns exactly one /permissions/me/ fetch for the whole app and hands every check an immutable snapshot:

import { RbacProvider } from "@archenstack/rbac-web/react";

<RbacProvider client={client}>
<App />
</RbacProvider>

Synchronous feature checks​

These resolve instantly from the shared snapshot — no request per check.

import { useCan, Can } from "@archenstack/rbac-web/react";

const canExport = useCan("reports.export", "write"); // "read" | "write" | "full" | a level
if (canExport) showExportButton();

// Hide unless allowed:
<Can feature="reports.export" need="write">
<ExportButton />
</Can>

// Or disable-with-reason — a function child owns HOW to disable:
<Can feature="records.edit" need="write">
{(d) => <EditButton disabled={!d.allowed} title={d.allowed ? "" : "You can't edit records"} />}
</Can>

Filter config-driven nav / routes by permission, too. useFilteredItems also returns loading, so a router can show a spinner (never a false "Forbidden") on a cold deep-link:

import { useFilteredItems } from "@archenstack/rbac-web/react";

const NAV = [
{ label: "Reports", to: "/reports", requires: { feature: "reports.view" } },
{ label: "Admin", requires: { feature: "rbac.manage" }, children: [/* … */] }, // empty section auto-collapses
];
const { items, loading } = useFilteredItems(NAV);

The framework-free equivalents (makeCan, decide, filterByPermission, readActions) are exported from the bare @archenstack/rbac-web entry, for route/config modules that aren't React.

No object argument — by design

The entire synchronous surface (useCan, <Can>, filterByPermission, …) takes no object parameter. There's deliberately no way to resolve per-object access on the client, so you can never accidentally trust a client-side object decision. Object decisions have exactly one source of truth: the server.

Server-backed object checks​

When access depends on which object (a PARTIAL, scoped write), ask the server:

import { CanObject, useCanObject } from "@archenstack/rbac-web/react";

<CanObject feature="records.edit" object={{ id: row.id, owner: row.owner, department: row.department }}>
<EditButton row={row} />
</CanObject>

The object dict must carry every attribute the feature's server-side scope reads (owner, department, …). Give it too little and the server fails closed — correctly hiding the control even from an authorized user — so pass the full object, not just an id.

Keep a list cheap​

A naive list issues one /check/ per row (an N+1). Prefer, in order:

  1. All-NONE rows render nothing — no request at all.
  2. A FULL grant shows the control with no check (the mutation still re-authorizes).
  3. Only a PARTIAL grant actually varies per object. Embed a per-row decision in your list endpoint and read it with readActions(row, "records.edit") for zero extra requests.
  4. If you can't change the backend, resolve the whole page in one round-trip:
import { useCanObjects } from "@archenstack/rbac-web/react";

const states = useCanObjects("records.edit", rows.map((r) => ({ id: r.id, owner: r.owner })));
rows.map((r, i) => states[i].allowed && <EditButton row={r} />); // positional: states[i] ↔ rows[i]

useCanObjects (and client.checkMany) send one batch POST /permissions/check/, serve TTL-valid cache hits locally, and batch only the misses. See the batch /check/ protocol.

The safety model, in short​

  • Fail closed everywhere. While /me/ is loading, on error, and for an unknown feature, every check denies. SSR renders the denied state — no grants leak into the HTML.
  • Objects are server-only. The synchronous checks can't take an object; object access comes only from the server's /check/ answer.
  • Self-healing. The provider re-fetches on window focus by default, so an out-of-band demotion clears on the next focus. Call refresh() (from useRbac()) after a login, a token rotation, or a matrix edit.

Server-side rendering (SSR)​

The permission layer is SSR-safe and fails closed on the server, so you can render it from Next.js (or any SSR framework) without leaking a single permission.

Internally, RbacProvider reads its snapshot through React's useSyncExternalStore, and its server snapshot is the empty, denied state. During SSR every check therefore denies, and no permission grants are ever serialized into the HTML — nothing about who-can-do-what ends up in the server-rendered markup, and there's no hydration flash of privileged UI. Once the app hydrates on the client, the provider fetches /permissions/me/ once and the checks fill in with the real answers.

For an SSR or same-origin app, use an httpOnly cookie session for auth rather than an in-memory bearer token: set credentials: "include" on the RbacClient (and send a CSRF header on writes). The server-rendered request then carries the session cookie automatically. (SPAs, by contrast, hold a bearer token in memory.)

// A Next.js client component that wraps your tree.
"use client";
import { RbacClient } from "@archenstack/rbac-web";
import { RbacProvider } from "@archenstack/rbac-web/react";

const client = new RbacClient({
baseUrl: "/api/v1",
credentials: "include", // send the httpOnly session cookie
});

export function Providers({ children }) {
return <RbacProvider client={client}>{children}</RbacProvider>;
}

The upshot: the server renders the safe, logged-out shell, and the client fills in the real permissions right after mount. Because the server always denies, design each check so its denied state degrades gracefully — a spinner or a hidden control, not a hard "Forbidden." useFilteredItems' loading flag exists for exactly this. For why the client is untrusted and what the server must re-check, see the security model.

See also​