Aviary

Current user

Who is authorized — the official getUser on routes, and for Gate calls without forUser, @nestjs/authentication's AuthenticationContext first, then nestjs-context with resolveUser, then a guest.

Authz never authenticates. It authorizes the user your authentication layer produced, read in one of three ways depending on where the check runs.

On routes: the official getUser

The official guard reads the user with the getUser given to AuthorizationModule.forRoot() (default request.user). byId(), RolePolicy, PermissionPolicy, the Inertia integration and the can-endpoint all see that same user. resolveUser never applies here.

AuthorizationModule.forRoot({ getUser: (ctx) => ctx.switchToHttp().getRequest().user });

currentUserOf(context) returns it from any ExecutionContext (null for a guest).

In services: gate.forUser(user)

The usual way to check in a service is to pass the user your handler received:

await this.gate.forUser(user).authorize(PostPolicy, 'update', post);

The value is used as is. forUser(null) (or undefined) is an explicit guest.

The ambient user (gate.allows(...) without forUser)

When no user is passed, the Gate looks, in order:

  1. @nestjs/authentication's AuthenticationContext.user — when that optional peer is installed and its module imported. Authz loads the package lazily and finds the AuthenticationContext through Nest's ModuleRef, so nothing breaks without it. It is an AsyncLocalStorage-backed context: correct in the handler and in everything it calls or awaits, however many requests run at once. Inside its scope (a call it handles, or AuthenticationContext.run()) its answer is final: an anonymous call is a guest, and nestjs-context is not asked. Outside any authentication scope (a queue worker, a cron job, a guard that runs before its interceptor), the next source is asked.

  2. @dudousxd/nestjs-context's user ref, only when there is no authentication scope — a { type, id } read through the CONTEXT_ACCESSOR token (consumed structurally; authz never imports nestjs-context). Configure resolveUser to hydrate it into your entity:

    AuthzModule.forRoot({
      resolveUser: (ref) => users.findOneBy({ id: ref.id }), // nullish = guest
    });

    The hydrated user is memoized on the request's context store by type:id, so a page that runs fifty checks calls resolveUser once. A call that throws is not cached.

  3. A guest (null).

Without resolveUser, the nestjs-context path hands policies and superAdmin the raw { type, id } ref, not your entity: user.isRoot is undefined there. Hydrate it, compare by id, or prefer forUser(user) / @nestjs/authentication.

RoutesforUser(user)Ambient Gate
Sourceofficial getUserthe value you passAuthenticationContext.user in its scope, else nestjs-context
resolveUser appliednonoonly on the nestjs-context ref
Needs@nestjs/authorizationnothing@nestjs/authentication or nestjs-context

Guests

A guest is null everywhere: official policies receive null (type the user User | null), ad-hoc gates deny a guest without running, gate.scope() gives deny-all, and authorize() throws a 401 AuthorizationError. superAdmin and a DECISION_PROVIDER are still asked, and may grant.

Next steps

On this page