Auth and permissions
How the server locks down every route by default, five global guards, handler decorators, CASL, and the JWT that carries your school.
The single most important thing to understand about auth on this server is that it is opt-out, not opt-in. You don't add security to a route: security is already there, and you remove it where you genuinely don't need it. Every request must carry a valid token and clear a chain of checks before your handler ever runs. A brand-new controller, with no decorators at all, is locked down by default.
That design choice has a direct consequence: a new controller with no decorators requires a bearer token. The failure mode is a 401, which you answer by saying who is allowed, or by marking the route @Public() when it really is public. @Public() is an opt-out, not the default. ApiKeyAuth applies that opt-out and then requires x-api-key.
Everything in this page lives under apps/server/src/app/auth/. Let's build the mental model from the outside in: the guards that gate every request, the decorators you sprinkle on handlers to tune them, and the token that ties a user to their school.
Two words you'll see constantly
Authentication is "who are you?": proving identity with a token. Authorization is "are you allowed to do this?": checking permissions. The first guard does authentication; the other three do authorization. Keep the two ideas separate in your head and the rest of this page clicks.
The global guards
A guard in NestJS is a small class that runs before your route handler and decides whether the request may proceed. Returning true lets it through; throwing (or returning false) stops it cold. Most apps attach guards route by route. This server attaches five of them globally, registered once as APP_GUARD providers in AuthModule, so they wrap every controller in the schools API.
The order in apps/server/src/app/auth/auth.module.ts is:
providers: [
// ...
{ provide: APP_GUARD, useClass: JwtAuthGuard },
{ provide: APP_GUARD, useClass: UserTypeGuard },
{ provide: APP_GUARD, useClass: SystemRoleGuard },
{ provide: APP_GUARD, useClass: PermissionsGuard },
{ provide: APP_GUARD, useClass: FeatureFlagGuard },
],They run top to bottom, and that order matters: each layer assumes the one before it already passed. There's no point checking your role before we know who you are. Here's what each one enforces.
1. JwtAuthGuard
This is the front door. It validates the Bearer token on the request (AuthGuard('jwt')) and, on success, attaches the decoded user to the request. It also returns friendly, specific messages, "Session has expired" versus "Authentication required", so the frontend can react sensibly.
Two special behaviours live here. First, it skips the whole check when the route is marked @Public(). Second, once the token checks out, it writes the user's tenant into the per-request context (more on that below). Everything downstream depends on this guard having run.
2. UserTypeGuard
The system has distinct user types (think staff vs. student vs. guardian vs. platform operator). This guard enforces @RequireUserType(...): a route decorated for staff simply rejects a student's token before any finer-grained check runs. If a handler has no @RequireUserType, this guard waves it through.
3. SystemRoleGuard
Some capabilities are gated by system-level roles: global roles that aren't tied to a particular campus. This guard enforces @RequireSystemRole(...). Keep this distinct from the campus-scoped roles you'll meet in the role-based access control (RBAC) layer; system roles are the broad, school-wide kind.
4. PermissionsGuard
This layer does CASL-based RBAC (covered in the next section): it builds the user's abilities and checks @RequirePermissions(...). If the user lacks even one required permission, the request is denied, and the denial is audit-logged with the IP and user-agent, because a blocked attempt is exactly the kind of thing you want a record of.
5. FeatureFlagGuard
FeatureFlagGuard runs last. When the feature named by @RequireFeature is off, it throws NotFoundException with error: 'FEATURE_DISABLED'. It returns immediately on a @Public() route, before it reads the feature metadata. A public method on a controller that also has @RequireFeature does not get that check. Status codes are on Authentication across the platforms.
Order is a safety property, not a detail
This diagram shows the order and what each guard returns when it refuses. A guard with nothing declared on the route passes. @Public() makes the token, system role, permission, and feature guards return early; UserTypeGuard still applies a @RequireUserType if one is declared.
The guards run JwtAuthGuard → UserTypeGuard → SystemRoleGuard → PermissionsGuard → FeatureFlagGuard. Because each step trusts that the previous one succeeded, reordering them would let a check run against an unauthenticated request. If you ever touch the APP_GUARD registration, preserve this order.
Decorators
The guards are generic. Decorators are how an individual handler tells them what it wants: they attach metadata that the guards read. You'll reach for five of them constantly.
| Decorator | What it does |
|---|---|
@Public() | Skip auth/permission guards entirely: login, one-time password (OTP), webhooks, health checks |
@RequirePermissions({ action, resource }) | Require a CASL ability; repeat it for AND logic |
@RequireUserType(...types) | Restrict the route to specific UserTypes |
@RequireSystemRole(...) | Require a system-level role |
@CurrentUser(property?) | Inject the decoded JSON Web Token (JWT) payload, or one field of it |
The two you'll use most are @RequirePermissions to gate the handler and @CurrentUser to read the caller. A typical protected handler reads like a sentence, "to create a student, you must have CREATE on STUDENT":
@Post()
@RequirePermissions({ action: Action.CREATE, resource: Resource.STUDENT })
create(@Body() dto: CreateStudentDto, @CurrentUser() user: JwtPayload) {
// `user` is the decoded token: user.sub, user.schoolId, etc.
return this.studentService.create(dto, user);
}@CurrentUser() with no argument hands you the whole payload; pass a property name, @CurrentUser('sub'), to inject just the user id. The decorator itself is tiny; it's defined in apps/server/src/app/auth/decorators/.
@Public() is the one with teeth
@Public() turns the guards off for that route. It exists for the handful of endpoints that genuinely can't require a token: logging in, the OTP exchange, health probes. Treat every new @Public() as a security decision, not a convenience: a public route still has to defend itself (throttling, input validation, no data leaks).
CASL permissions
The PermissionsGuard is powered by CASL, a small authorization library. Its model is wonderfully simple: a permission is an (action, resource) pair. Can this user CREATE a STUDENT? Can they READ an AUDIT_LOG? For each request, an AbilityFactoryService assembles the user's full set of abilities, and the guard checks that the user can(action, resource) for every permission the route requires.
Both halves of the pair are enums, defined in libs/shared/src/permission/permission.enum.ts:
enum Action { READ, LIST, CREATE, UPDATE, DELETE, MANAGE, APPROVE, IMPERSONATE }
enum Resource { ALL, USER, STAFF, STUDENT, GUARDIAN, GRADE, CLASS, APPLICATION,
REPORT_CARD, ROLE, CAMPUS, DEPARTMENT, SUBJECT, /* ... */ }The snippet is the start of each enum, not the full list. ACADEMIC_REPORT is not a Resource value. The report-card resource is report_card. Every resource and the actions each one actually gets are on Permission registry.
Import them from the permission subpath so you get the right symbols:
import { Action, Resource } from '@app/shared/permission';Two actions deserve a footnote. MANAGE is CASL's wildcard: it means "every action on this resource". And ALL is the wildcard resource. Together, MANAGE + ALL is effectively "can do anything", which is why it's handed out sparingly.
This is the surface, not the depth. How abilities are computed per user type, how roles map to permission sets, and how access is narrowed to a single campus or even a single row, all belong to the RBAC subsystem: that detailed material, along with role administration, is documented later in the API domains section, so we won't dive into it here.
The permission cache
Building a person's permission context means reading roles, departments, teaching assignments and profiles, so each one is cached in the cache Redis (CACHE_REDIS_URL) for 10 minutes (since API 1d66cf95, 30 October 2026). Lesson-planning lists also take the person's taught subjects and class-teacher streams from it.
| Part | How it works |
|---|---|
| Key | perm:{version}:{school}:{user}:{campus} |
| Clearing | PermissionVersionSubscriber watches every table the context reads (roles, role permissions, user roles, class limits, departments and members, subject and class teachers, classes, streams, academic years, guardian links, all four profiles, and user deletes). Any write through TypeORM changes perm:version, so every cached context is ignored at once |
| Not clearing | Profile edits to rover, pipShape or profileImageId only |
| Redis down | A read or write failure is a miss; requests use the database. If changing the version fails, the API skips the cache for 10 minutes rather than risk stale permissions |
Raw SQL doesn't clear it
The subscriber only sees writes made through TypeORM entities and query builders. A raw dataSource.query that changes a role, assignment or profile won't bump the version, so people keep their old permissions for up to 10 minutes. Write through the repository, or call PermissionCacheService.bump() yourself.
schoolId in the JWT
This server is multi-tenant: every ordinary user belongs to exactly one school, and a request must never leak data across schools. The mechanism that makes that cheap and reliable is putting the tenant inside the token. The payload shape lives in auth/dto/auth.types.ts:
interface JwtPayload {
sub: string; // user id
email: string;
type: UserType;
profileId: string;
schoolId?: string; // owning school (tenant): absent for platform admins
campusId?: string;
act?: string; // real admin's id during an impersonation session
iat: number;
exp: number;
}Notice schoolId is optional. It's stamped on the token for every tenant user, but omitted for platform_admin operators, who run the platform itself and belong to no single school. The token service sets it conditionally, present only when the user actually has one:
...(user.schoolId && { schoolId: user.schoolId }),Because the school travels inside a signed token, no handler has to ask "which tenant is this?": the answer is already proven and tamper-proof.
Tenant context
A token tells you the tenant, but your repository code, buried several layers deep, also needs it, and you don't want to thread schoolId through every function call. The solution is AsyncLocalStorage: a Node feature that holds a value for the lifetime of one request, readable anywhere downstream without passing it around. The server keeps the current tenant there.
It's set up in two moves. First, a global middleware opens the context with a fail-closed default before the request even reaches NestJS, schoolId: null, no bypass:
tenantContextStore.run(
{ schoolId: null, isPlatformOperator: false, bypass: false },
() => next(),
);Then, once JwtAuthGuard validates the token, it mutates that context in place, writing the real schoolId for a tenant user, or setting bypass: true for a platform operator who is allowed to read across tenants.
Why mutate instead of re-entering the context?
Passport's verify callback runs on an async branch, and starting a fresh context there (enterWith) wouldn't propagate back to your handler. Mutating the object the middleware already run()-bound does reach the handler. Hence the two-step dance: a fail-closed default up front, an in-place update once identity is known.
The payoff of fail-closed: a route that never establishes a real tenant, say a @Public() one, has schoolId: null, so the data layer can't silently hand back another school's rows. To read across tenants you must opt in explicitly. The repository-scoping side of this story (@TenantScoped, runUnscoped(), withSchool()) is covered in The Database.
Logging in
Authentication produces an access token and a refresh token. The schools API has password login, OTP, magic link, and Microsoft. Bodies, throttles, and which user types each door accepts are on Authentication across the platforms. Magic link is guardians and guests only. It is not a fourth door for staff or platform admins.
Password login checks the field identifier plus a password. OTP login is passwordless: an access code or email, a 6-digit code by email, then the token pair. PlatformAdminSeedService stores passwordHash: '' and its comment says operators use OTP-by-email. Do not describe password login as their path. Microsoft 365 login is a tenant-scoped OAuth round-trip: GET auth/microsoft?schoolId=<id> kicks it off and the callback exchanges the code for a profile. Microsoft is the one path that doesn't use a Passport strategy; it's wired up by hand.
On API staging ed0e56f87083d7d10466b61b97948f7e855c1c03 (merge of PR #686, 29 November 2026, 02:17 GMT), not on main d0fbb403, microsoftLogin calls findByEmailWithDeleted when no live user matches the Microsoft id or the email. A row with deletedAt set is 403 This account was removed. Ask the school office for help. It writes an ACCESS audit deny, action LOGIN_MICROSOFT, reason Account removed. That path does not call Graph and does not create a user. This method does not restore the row. A search of user.controller.ts on the API default branch found no restore route, and a Hub search found no user-restore screen. Directory sync's restore of a soft-deleted match was not re-read at this commit. auth.service.ts and its test were read. The test was not run. A merge to staging is not a running deploy.
The OTP endpoints are all @Public() and throttled, and they're carefully built to never leak whether an account exists:
| Route | Operation | Notes |
|---|---|---|
POST auth/otp/lookup | lookupLoginOtp | Type-ahead match; returns a masked email or found: false, never errors on a miss |
POST auth/otp/request | requestLoginOtp | Emails a 6-digit code; invalidates any prior live code |
POST auth/otp/verify | verifyLoginOtp | Exchanges a valid, single-use code for a token pair |
OTP codes are hashed and single-use
A login code is never stored in plain text, only a SHA-256 hash, alongside an expiresAt, a consumedAt, and an attempts counter. Codes expire, can be used once, and have a capped attempt count. If you work on this flow, preserve those properties.
Refresh-token rotation
Access tokens expire fast (default 15 minutes), so the client trades a refresh token for a fresh pair when one lapses. The refresh token is rotated on every use, and the old one is revoked before new tokens are minted. Refresh tokens are stored only as a SHA-256 hash, each tagged with a familyId.
That familyId enables replay detection: if someone presents an already-revoked token, the server assumes it was stolen and revokes the entire family, forcing a clean re-login. One more guard rail: if the user's school is suspended, both login and refresh are rejected with 403 before any token is touched, so a denied refresh never burns the family.
On API staging 6308a8a1 (not on main d0fbb403), TokenService also refuses a deactivated account or a profile whose isActive is false, with 403 This account is suspended. The check runs when a token is minted, including refresh. An impersonation mint (actorId is set) skips it, and so does a refresh of an impersonation token. An access token already issued is not revoked. It stays valid until it expires. This is separate from School is suspended. The route limits that can return 429, and the daily cap of 30 wrong sign-in codes, are on Authentication across the platforms. token.service.ts was read. The token test was not run. A merge to staging is not a running deploy.
Where to go next
The Database
The repository-scoping side of multi-tenancy: how schoolId filters every query.
Module Anatomy
Where controllers, guards, and decorators sit inside a feature module.
API Conventions
How routes, operationIds, and DTOs are shaped across the server.
Add an Endpoint
Put it together: add a guarded route with the right decorators.