Authorization & multi-tenancy
Role vs resource-based checks, enforcing tenant_id on every query, row-level security. The classic probe: "how do you stop user A reading user B's invoice?"
The mental model
Authentication asked *who are you*; authorization asks the harder question: are you allowed to touch this specific thing? The gap between those two is where almost every real data breach in SaaS lives — not "an attacker got in" but "a legitimate user read a row that wasn't theirs by changing a number in a URL." Authorization is the discipline of making sure every query returns only rows the caller is entitled to, and in multi-tenant SaaS that means every query is scoped by who is asking and which tenant they belong to.
The mental model that prevents the disasters: **authorization is a property of data access, not a checkpoint you pass once.** A junior imagines a gate at the door — "is the user logged in and an admin? yes → let them into the app." But the real question is asked per-row, per-request: this user, on this endpoint, for *this* invoice id — may they see it? If your authorization is a single gate at the top, then once past it a user can request any id and get any row. The defense has to live *in the query itself*: WHERE id = $1 AND org_id = $callers_org. A mismatched id then returns nothing, not someone else's data.
The analogy: think of a multi-tenant database as a **shared office building where every company rents a floor**. Authentication is the lobby turnstile — it proves you work in the building. But it does not, by itself, stop you walking onto another company's floor and reading their files. Tenant isolation is the keycard that only opens *your* floor, enforced at *every* door, not just the lobby. The catastrophic bug is a building where the turnstile is strict but the internal doors are unlocked — which is exactly what an app is when it authenticates carefully and then runs SELECT * FROM invoices WHERE id = $1 with no tenant scope.
Why this exists as its own topic: in a shared-schema SaaS, one forgotten WHERE org_id = ... leaks every tenant's data through a single endpoint. The blast radius of an authorization mistake is the entire customer base, which is why "how do you stop user A reading user B's invoice?" is the classic senior probe.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision