@telamonjs/permissions
RBAC materialized into the JWT. Permissions are resolved once at login and
carried in the token, so every check is a pure array lookup on the claims:
no database query per request. The core is client-safe (fine inside a
React component); @telamonjs/permissions/nest adds a @Permissions(...)
decorator and a guard that reads the same array off request.user.
Zero runtime dependencies · dual ESM/CJS · sideEffects: false.
Install
pnpm add @telamonjs/permissions
| Peer dependency |
Range |
@nestjs/common (optional) |
^10.0.0 \|\| ^11.0.0 |
@nestjs/core (optional) |
^10.0.0 \|\| ^11.0.0 |
The Nest peers are only needed for @telamonjs/permissions/nest.
Client-safe checker, once per app
import { createPermissionChecker } from '@telamonjs/permissions'
export const { can, canAny, canAll, hasBypassRole } =
createPermissionChecker({ bypassRoles: ['superadmin'] })
can(user, 'users.write') // single permission
canAny(user, ['a.read', 'b.read']) // OR
canAll(user, ['a.read', 'a.write']) // AND
user is anything shaped like UserWithPermissions
({ id, role?, roles?, permissions? }). The AuthUser from
@telamonjs/auth satisfies it
by design: a duck-typed contract pinned by a test, with no dependency between
the packages.
NestJS guard and decorator
import { createPermissionsGuard, Permissions } from '@telamonjs/permissions/nest'
export const PermissionsGuard = createPermissionsGuard() // bypassRoles defaults to ['superadmin']
@Get('admin')
@UseGuards(JwtAuthGuard, PermissionsGuard) // auth first: it sets request.user
@Permissions('admin.read', 'admin.write') // string clauses are OR
getAdminData() {}
@Permissions(['admin.read', 'admin.write']) // an inner array is AND
The guard delegates to the same checker, so client and server checks can't drift.
A request with no request.user gets a 401; a user without the permission gets a
403 with the generic message Insufficient permissions (the required codes are
never sent to the client).
By default a route without @Permissions is open. Opt into deny-by-default with
failClosed; then only routes marked @Public() skip the decorator:
import { createPermissionsGuard, Public } from '@telamonjs/permissions/nest'
export const PermissionsGuard = createPermissionsGuard({ failClosed: true })
@Public() // same metadata key as Public() from @telamonjs/auth/nest
@Get('health')
health() {}
Two more options:
onDenied({ user, required, request }) is called right before every 403
(log the denial there). The client still gets the generic message.
createException(status, message) builds the 401/403. Set it while the
package is linked (link:/file:), so the guard throws the host's
@nestjs/common classes instead of its own copy's, which the host's filters
would turn into a 500:
import { ForbiddenException, UnauthorizedException } from '@nestjs/common'
export const PermissionsGuard = createPermissionsGuard({
onDenied: ({ user, required }) => logger.warn('permission denied', { userId: user?.id, required }),
createException: (status, message) =>
status === 401 ? new UnauthorizedException(message) : new ForbiddenException(message),
})
Role helpers
isAdminRole(role, roles) // 'admin' | 'superadmin'
isSuperAdminRole(role, roles) // privilege gates only
hasGlobalDataVisibility(role, roles) // data scoping only: don't mix the two
getPrimaryRole(role, roles)
Multi-role first: roles[] wins everywhere, and the singular role is the fallback.
Reference Prisma schema
A starting-point schema ships with the package. Copy it into your
schema.prisma and adapt it: it is a reference, not a managed migration.
cat node_modules/@telamonjs/permissions/src/prisma/schema.prisma
The model and enum names are in Portuguese (pt-BR): Papel (role),
UsuarioPapel (user ↔ role), Permissao (permission), PermissaoPapel
(role ↔ permission), PermissaoUsuario (per-user grant/revoke overrides) and
LogPermissao (audit log), plus the enums AcaoPermissao, TipoOverride and
ResultadoPermissao. Rename them freely in your copy; the checker only reads
the permissions[] claim.
This package checks permissions. Resolving them at login (database →
permissions[] claim) is your app's job, typically in your NextAuth
authorize() or API login, feeding @telamonjs/auth's session claims.
Entry points
@telamonjs/permissions
@telamonjs/permissions/nest
@telamonjs/permissions/prisma/schema.prisma (file ./src/prisma/schema.prisma)
Every name each entry point exports, runtime and types: Public API index.
Full docs
Full docs → the permissions page of the docs site
(MDX sources, readable on GitHub).