Skip to content
Backend & APIChapter 29 / 57Advanced7 min read

Authorization & Multi-Tenancy

Authorization for multi tenant apps with RBAC. Check on the server for every request and resource, take the tenant id from the session, and avoid IDOR data leaks.

On this page

1. What is this practice?

Authentication tells you "who you are". Authorization tells you "what you can do". Multi-tenancy means one app and one database hold the data of many companies (tenants), and they must never see each other's data.

2. Why does it matter?

The most common bug is called IDOR (Insecure Direct Object Reference). A user opens their own invoice: /api/invoices/1042. They change the URL to 1043 and another company's invoice opens. There was a login check, but no ownership check.

Another case: a dashboard took the tenant id from the query string, ?tenantId=7. Someone changed it to 8 and saw another company's whole dataset. A third case: a profile update did update(body), and a user sent role: "admin" in the body and became an admin.

Login check onlyCheck on every resource
Change the id and anyone's record opensTenant or owner filter in the query, otherwise 404
Tenant id from the URL or bodyTenant id from the session or host
Role can be set from the bodyRole only from the database or a verified token
Admin routes guarded one by oneOne guard on the whole admin prefix
401 when signed in but refused403 when refused, 401 when login is missing

3. When to follow it

LevelHow importantWhy
Learning projectLearning ownership checks is essentialThis is the first security bug in every CRUD app
Production codeRequiredOne missing filter leaks the whole customer list
Senior levelChoose and document the tenancy modelCode filters or database policies, the team must know which

When to learn it: after Authentication Patterns, once the app holds data for more than one user.

4. Bad vs good code

Bad: login check, no ownership

ts
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
  const user = await getCurrentUser();
  if (!user) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const { id } = await params;
  const tenantId = new URL(request.url).searchParams.get("tenantId");
  const invoice = await db.query("select * from invoices where id = $1", [id]);

  return NextResponse.json({ invoice: invoice.rows[0], tenantId });
}

Any signed in user can read any invoice just by typing its id.

Good: tenant from the session, filter in the data layer

File lib/invoices/invoices.queries.ts:

ts
export async function findInvoiceForTenant(tenantId: string, invoiceId: string) {
  const result = await pool.query<InvoiceRow>(
    "select id, number, total, status from invoices where tenant_id = $1 and id = $2",
    [tenantId, invoiceId],
  );

  return result.rows.at(0);
}

Handler:

ts
export async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) {
  const membership = await getCurrentMembership();
  if (!membership) {
    return NextResponse.json({ error: INVOICE_MESSAGES.unauthorized }, { status: 401 });
  }

  const { id } = await params;
  const invoice = await findInvoiceForTenant(membership.tenantId, id);
  if (!invoice) {
    return NextResponse.json({ error: INVOICE_MESSAGES.notFound }, { status: 404 });
  }

  return NextResponse.json({ invoice: toInvoice(invoice) });
}

For roles, one closed enum, in one file:

ts
export const ROLES = ["owner", "admin", "member"] as const;
export type Role = (typeof ROLES)[number];

export function canManageBilling(role: Role) {
  return role === "owner" || role === "admin";
}

If you use Postgres Row Level Security (RLS), the database applies the filter itself:

sql
alter table invoices enable row level security;

create policy invoices_select_own_tenant on invoices
  for select
  using (tenant_id in (select tenant_id from memberships where user_id = auth.uid()));

What is the difference?

ThingBad codeGood code
TenantFrom the query stringFrom the session membership
Query filterOnly idBoth tenant_id and id
Someone else's recordOpens404
Where the query livesIn the handlerIn the data layer
ResponseThe whole rowA toInvoice mapper

5. How to apply it

Layers of authorization
Resolve the caller (401 if missing)
Take the role and tenant from the session or database
Check that the action is allowed (403 if not)
Add the tenant or owner filter to the query
If no record is found, return 404

There are two tenancy models. Write down in the app's docs which one is used:

ModelHowCondition
Filter in codeThe server's privileged connection, tenant_id in every queryAll queries live only in the data layer, and a query outside it is a review blocker
Row Level SecurityPolicies on every table, queries run with the user's tokenA policy on every table, and the admin key only in webhooks and jobs

A few more rules:

  • Put admin routes under one prefix and guard the whole group, so a new route cannot forget the guard.
  • Admin actions that change customer data go into an audit table: who, what, when.
  • Check and increment a quota in one statement: update ... where used < limit.
  • Authorize realtime channels at subscribe time, and make sure broadcasts carry no other tenant's data.

6. Exceptions and common mistakes

Sometimes returning 404 instead of 403 is better so an attacker cannot tell that the record exists, like in the example above. Do this on purpose and as a team decision. Public data, such as blog posts, needs no ownership check, but draft posts do.

Mistake: Checking only at the start of the route

Why? The user is logged in, but the record belongs to someone else, and the check gets forgotten a line later.

Better: Add the owner or tenant filter to the query every time you load a resource.

Mistake: Keeping the role list in two files

Why? A new role is added in one file but not the other, and a permission check quietly becomes wrong.

Better: One closed enum of roles in one file, and an enum or CHECK constraint in the database too.

Mistake: A permissions table the server never checks

Why? The UI hides buttons but the API call still goes through, so it is only decoration.

Better: Enforce every permission that exists on the server, or remove it.

Mistake: Using a database client with the admin key in user requests

Why? RLS policies are bypassed and a single bug exposes the data of every tenant.

Better: Keep the privileged client in a server only module and use it only in webhooks or admin jobs.

7. What to learn before and after

Final quick revision

CheckQuestion
Every resourceDoes the query have a tenant or owner filter?
Tenant sourceDoes the tenant id come from the session or host?
RoleDoes the role ever come from the body or a client flag?
StatusMissing login is 401, refused is 403?
AdminIs there one guard on the admin group, and an audit log?
Tenancy modelDo the docs say filter or RLS?