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:
-
@nestjs/authentication'sAuthenticationContext.user— when that optional peer is installed and its module imported. Authz loads the package lazily and finds theAuthenticationContextthrough Nest'sModuleRef, so nothing breaks without it. It is anAsyncLocalStorage-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, orAuthenticationContext.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. -
@dudousxd/nestjs-context's user ref, only when there is no authentication scope — a{ type, id }read through theCONTEXT_ACCESSORtoken (consumed structurally; authz never imports nestjs-context). ConfigureresolveUserto 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 callsresolveUseronce. A call that throws is not cached. -
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.
| Routes | forUser(user) | Ambient Gate | |
|---|---|---|---|
| Source | official getUser | the value you pass | AuthenticationContext.user in its scope, else nestjs-context |
resolveUser applied | no | no | only on the nestjs-context ref |
| Needs | @nestjs/authorization | nothing | @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
Enforcement
Routes are guarded by the official deny-by-default AuthorizationGuard. This page covers our helpers for the official @Can, the @Roles / @Permission sugar, the decision channel and the optional can-endpoint.
Resource resolution
byId(Resource) loads the record the official @Can passes to your ability — the default IdParamResourceResolver, a custom ORM-backed ResourceResolver, authorizedResource, and resourceLoaders for the can-endpoint.