Ecosystem & integrations
The glue packages that make authz disappear into your stack — context, Inertia, codegen, React, Telescope, and the filter integration.
Decisions belong to the official @nestjs/authorization. The leverage of authz comes from the glue — a set of companion packages that wire authorization into the rest of your stack so you write a rule once and it shows up everywhere: in your guards, in your typed client, in your React components, and in your observability tools.
The guiding principle across the ecosystem: the integration lives on the side that already has the import. You never install a bridge package just to connect two libraries you already use; the consumer reads optionally, the producer carries the data when present, and in most apps you wire nothing at all.
Current user — @nestjs/authentication and @dudousxd/nestjs-context
Routes use the official getUser. For Gate calls without forUser, authz reads
@nestjs/authentication's AuthenticationContext.user inside its scope (where null is a
guest), and only outside it @dudousxd/nestjs-context's user ref (hydrated by
resolveUser). nestjs-context
also carries the per-request permission cache and the tenant for tenant-scoped roles. Both are
optional peers, consumed without a hard import. See Current user.
Inertia — @dudousxd/nestjs-authz-inertia
If you render with Inertia, the authz Inertia integration injects an auth.can map into your shared props — in the spirit of Laravel's HandleInertiaRequests. It holds the class-level abilities of every official @Policy() (keyed <resource>.<method>), checked through AuthorizationService for the user the official guard reads; the denial filter redirects a 401 to the login page and a 403 to forbiddenUrl:
// In a React/Vue page, props.auth.can was populated server-side
{auth.can('update', post) && <EditButton post={post} />}The server evaluates them during the Inertia response, so the client never asks again for the data it already has.
Codegen — @dudousxd/nestjs-authz-codegen
The codegen integration emits a typed can() into your generated client (api.ts). It reads the official @Can(Policy, 'ability', byId(Resource)), @Permission / PermissionPolicy names, GatePolicy abilities and @Policy() / @PolicyFor() classes. An ability's resource comes from byId(Resource) or the policy's @PolicyFor link; an unlinked policy's abilities have no resource. Because the ability names come from your policies, asking for an ability that doesn't exist becomes a compile-time error rather than a silent false:
api.can('update', post); // ✅ ok
api.can('upadte', post); // ✗ TypeScript error — 'upadte' is not a known abilityThis closes the usual gap where frontend authorization strings drift out of sync with the backend rules they mirror.
React — @dudousxd/nestjs-authz-react
For React apps that aren't using Inertia, @dudousxd/nestjs-authz-react provides a hook and a gating component:
import { useCan, Can } from '@dudousxd/nestjs-authz-react';
function PostActions({ post }) {
const canEdit = useCan('update', post);
return (
<>
{canEdit && <EditButton post={post} />}
<Can ability="delete" of={post}>
<DeleteButton post={post} />
</Can>
</>
);
}<Can ability="..." of={...}> renders its children only when the ability is granted, keeping authorization declarative in your component tree.
Diagnostics & Telescope — @dudousxd/nestjs-diagnostics
Authz emits every authorization decision — each requirement the official guard enforced (reason: 'route') and each Gate check, allowed and denied, with the ability, reason, policy, handler and user — as a standard diagnostics event on the aviary:authz:decision channel, via @dudousxd/nestjs-diagnostics (Node's built-in diagnostics_channel under the hood). It costs essentially nothing when nothing is listening, and the payload rides the standard envelope with traceId auto-filled from nestjs-context.
To see those decisions in Telescope, add the one generic diagnostics watcher — @dudousxd/nestjs-diagnostics-telescope, which records every aviary:<lib>:<event> from any library, with no per-library watcher to wire:
import { TelescopeModule } from '@dudousxd/nestjs-telescope';
import { nestjsDiagnosticsTelescope } from '@dudousxd/nestjs-diagnostics-telescope';
TelescopeModule.forRoot({
extensions: [nestjsDiagnosticsTelescope()],
});Now every unexpected 403 lands in Telescope tagged lib:authz / event:decision, showing exactly which policy method ran and why it denied — authorization failures stop being opaque ("forbidden — but why?").
Typed client — @dudousxd/nestjs-authz-client
The client package provides the runtime that the codegen output builds on — the small surface your frontend calls to evaluate abilities against the backend (for cases where the answer can't be precomputed and shipped with the page).
Testing — @dudousxd/nestjs-authz-testing
buildGate() / buildBoundGate() compile a real Nest testing module with the official
AuthorizationModule and AuthzModule, so tests run the same decisions as production;
expectCan(gate, user, PostPolicy, 'update', post) / expectCannot(...) assert them,
FakePermissionProvider / FakeRoleProvider stand in for an adapter, and
requirementsOf(Controller, 'method') lists what the official guard will enforce on a route.
Query scoping
A policy's scope(user, entity) (or its viewAny) becomes a parameterized WHERE through the
ORM adapters' applyScope, so a listing returns only the records the user may see. A
DECISION_PROVIDER can supply the plan instead (Cerbos PlanResources). See
Gates.
Putting it together
A single ability you write in a PostPolicy.update method can, with the glue in place:
- guard the route via the official
@Can, - decide whether the Edit button renders (Inertia props or the React
<Can>), - be type-checked in your generated client (codegen),
- and leave an auditable trail when it denies (Telescope).
One rule, enforced consistently from the database to the button. That consistency — not the engine itself — is the point of authz.