Getting Started
Install @nestjs/authorization and @dudousxd/nestjs-authz, import both modules, write an official policy, guard a route, and check abilities in a service.
This guide takes an empty NestJS app to a guarded route and a service-layer check. The
decisions are the official @nestjs/authorization's;
this package adds the helpers around them.
Prerequisites
- NestJS 12 and Node 24+
- An authentication layer that puts the user on the request (
@nestjs/authentication, Passport, a JWT guard, sessions…). Authz authorizes; it does not authenticate.
Install
pnpm add @nestjs/authorization @dudousxd/nestjs-authzPeers: @nestjs/common, @nestjs/core, reflect-metadata, rxjs. Two optional peers:
@nestjs/authentication (its AuthenticationContext.user is the Gate's ambient user) and
@dudousxd/nestjs-context (a fallback ambient user, the per-request
permission cache, tenant-scoped roles).
Write an official policy
A policy is an official @Policy() class; each method is an ability that receives
(user | null, ...args) and allows only on a literal true.
// src/post.policy.ts
import { Policy } from '@nestjs/authorization';
import { AuthzPolicy, PolicyFor } from '@dudousxd/nestjs-authz';
import { Post } from './post.entity.js';
@Policy()
@PolicyFor(Post) // optional: links Post → PostPolicy for the extras
export class PostPolicy extends AuthzPolicy { // optional: honors superAdmin
view(user: User | null, post: Post) {
return post.published || post.authorId === user?.id;
}
update(user: User | null, post: Post) {
return post.authorId === user?.id;
}
create(user: User | null) {
return user?.verified === true; // no record needed
}
}Import the modules
// src/app.module.ts
import { Module } from '@nestjs/common';
import { AuthorizationModule } from '@nestjs/authorization';
import { AuthzModule } from '@dudousxd/nestjs-authz';
@Module({
imports: [
AuthorizationModule.forRoot({ getUser: (ctx) => ctx.switchToHttp().getRequest().user }),
AuthzModule.forRoot({
superAdmin: (user) => user?.isRoot === true,
// resourceResolver: new OrmResourceResolver(), // for byId(); default builds { id } from :id
}),
],
providers: [PostPolicy], // official policies are discovered wherever they are providers
controllers: [PostsController],
})
export class AppModule {}AuthzModule registers RolePolicy, PermissionPolicy, GatePolicy, the Gate, the resource
resolver byId() uses, and the allow + deny decision event. The official module must stay
global (its default). Without AuthorizationModule.forRoot() the app fails to start, with an
error naming the missing import.
Guard routes
// src/posts.controller.ts
import { Controller, Patch, Post as HttpPost, Req } from '@nestjs/common';
import { Can } from '@nestjs/authorization';
import { authorizedResource, byId } from '@dudousxd/nestjs-authz';
@Controller('posts')
export class PostsController {
@Patch(':id')
@Can(PostPolicy, 'update', byId(Post)) // loads the Post named by :id; 403 if not found
update(@Req() req: Request) {
const post = authorizedResource(req, Post);
}
@HttpPost()
@Can(PostPolicy, 'create') // no args: nothing is loaded
create() {}
}The official guard is deny-by-default: every handler needs a @Can(), @Can.Anyone() or
@Public().
Check in a service
import { Injectable } from '@nestjs/common';
import { Gate } from '@dudousxd/nestjs-authz';
@Injectable()
export class PostsService {
constructor(private readonly gate: Gate) {}
async publish(user: User, post: Post) {
await this.gate.forUser(user).authorize(PostPolicy, 'update', post); // = AuthorizationService.authorize
await this.gate.forUser(user).authorize('update', post); // the policy linked to Post
}
}authorize() throws the official AuthorizationError (401/403 when it leaves a handler).
Without forUser, the Gate uses the ambient user.
Next steps
Authz
Extras on top of NestJS's first-party @nestjs/authorization — persisted RBAC as official policies, @Can helpers, a service-layer Gate, super-admin, external engines and query scopes.
Policies
Official @nestjs/authorization policies, plus what this package adds — @PolicyFor, the AuthzPolicy base with superAdmin and the external engine, and PolicyIndex.