How It Works

Designing a Multi-Tenant SaaS: Enforcing Tenant Isolation at Three Layers

A walkthrough of isolating organizations using JWT claims, NestJS guards and a tenant-scoped query layer, so one customer can never read another's data.

TIMO360 logoTIMO360 Team1 min read
Designing a Multi-Tenant SaaS: Enforcing Tenant Isolation at Three Layers

Key takeaways

  • Every organization-owned document carries an organizationId
  • The token, a guard and the query layer each enforce the boundary independently
  • Controllers never trust an organizationId sent by the client
  • Super Admin routes are a separate, explicitly marked set

Why tenant leaks happen

Cross-tenant leaks are rarely dramatic. They come from one query that forgot a filter, or one endpoint that trusted an id from the request body. A single line of defence is one mistake away from a breach.

Layer one and two: the token and the guard

The access token embeds the user id, role and organizationId. A scope guard reads that claim and places it on the request context. From then on, handlers use the context value and ignore anything the client sends.

Layer three: the query

Services follow a base pattern that requires an organization id on every find, update and delete against tenant-scoped collections. Even if a guard were misconfigured, the data layer would still refuse an unscoped query.

Keeping Super Admin separate

Platform administrators need to see across organizations, so their endpoints bypass scoping. They live in a distinct, clearly marked set of routes guarded by the SUPER_ADMIN role only, which keeps the exception visible and reviewable.

Ready for Attendance & Presence Tracking That Respects Your Team?

Set up your organization, install the agent, and start seeing attendance and presence data in minutes — without a single keystroke or screenshot recorded.

Windows, macOS & Linux Pay per employee license Zero keystroke or screen capture
Chat with us