November 22, 2026 · 8 min read · Thomas Osayi
Multi-tenant SaaS with Firestore: data layout, security rules and custom claims
How to build a multi-tenant SaaS on Firestore: orgs collection vs tenantId field, per-tenant security rules, custom claims and the query mistakes that leak or block data.
In a multi-tenant SaaS, every customer shares one database and none of them may ever see another's data. On a traditional backend you enforce that in server code. On Firestore, the browser and the phone talk to the database directly, so the only thing standing between customer A and customer B's records is your security rules.
That changes how you design the data. I built SQWAD on Firebase with brands, their team members and creators separated and each brand's data isolated, and the lesson I took from it is the same one I would give anyone starting out: decide the tenant layout on day one, because moving it later means migrating every document. Here is how I choose a layout, write the rules and use custom claims without hitting their limits.
What does multi-tenant mean in a Firestore app?
It means one Firebase project and one Firestore database serve many customer organizations, and every read and write is checked against the organization the user belongs to.
The tenant is whatever your customer is: a company, a brand, a clinic, a team. Users belong to one or more tenants, and almost every document in the product belongs to exactly one. The app shows the user their tenant's data, but that is presentation. The real boundary is a rule like "allow this read only if the requester is a member of the org that owns this document."
Three things have to agree for that to work: where the tenant ID lives on each document (or in its path), where membership is recorded, and how the rules connect the two. Get those right and the rest of the app is ordinary Firestore work.

Should tenants be a top-level orgs collection or a tenantId field?
For most SaaS products I put each tenant under orgs/{orgId} with subcollections, and I use a flat collection with a tenantId field only when I need frequent queries across all tenants.
Option A: orgs/{orgId} with subcollections
Best for: B2B tools where users work inside one organization at a time.
Layout: orgs/{orgId}, orgs/{orgId}/members/{uid}, orgs/{orgId}/projects/{projectId}, orgs/{orgId}/invoices/{invoiceId}.
Pros: the tenant is in the path, so a single rule block covers everything under an org. Queries are naturally scoped. Deleting or exporting one customer is easy to reason about.
Cons: deleting an org document does not delete its subcollections, so offboarding needs a server function that walks them. Cross-tenant reporting needs collection group queries.
Option B: flat collections with a tenantId field
Best for: products where an internal admin view constantly queries across tenants, or where documents move between tenants.
Layout: projects/{projectId} with a tenantId field, invoices/{invoiceId} with a tenantId field.
Pros: simple cross-tenant queries for your own back office.
Cons: every rule must compare resource.data.tenantId, every create must validate request.resource.data.tenantId, and every client query must filter on it. Forget once and you have a leak or a broken screen.
Option C: one Firebase project per tenant
Best for: contracts that require physical separation. It multiplies deploys, indexes, keys and billing accounts, so I avoid it for an MVP.
How do I write security rules per tenant?
Check that the signed-in user has a membership document in the org they are trying to read, and check their role in that membership before any write.
With Option A, the core is a helper function and one match block. In plain text, the rule reads like this:
function isMember(orgId) returns exists(/databases/$(database)/documents/orgs/$(orgId)/members/$(request.auth.uid))
match /orgs/{orgId}/projects/{projectId} allows read if isMember(orgId), and allows write if isMember(orgId) and that member document has role equal to admin or editor.
Two details from the Firebase rules documentation matter here. First, every get() or exists() call in a rule performs a document read that you are billed for, even when the request is denied. Second, there is a limit of 10 such document access calls for single-document requests and queries, and 20 for multi-document reads, transactions and batched writes. Exceed it and the request fails with a permission error. A rule that looks up the org, then the member, then a plan document, then a project owner is already at four, so keep lookups shallow.
Also lock down the membership documents themselves. If a user can write their own members/{uid} document, they can add themselves to any org. Create memberships only from a Cloud Function or an invite flow that the rules verify.
When should I use custom claims for tenant access?
Use custom claims for small, stable facts such as a user's single org ID or a platform admin flag, and keep anything that changes often or grows with the user in Firestore.
Custom claims are set with the Admin SDK and travel inside the user's ID token, so rules can read them as request.auth.token.orgId without a billed read and without using one of the 10 document access calls. That makes them attractive. The Firebase documentation lists the catches:
- The claims payload must not exceed 1000 bytes. A user who belongs to forty orgs will not fit.
- Claims reach the client only when a new ID token is issued. If you remove someone from an org, their old token still carries the claim until it refreshes, so revoke tokens or force a refresh with getIdToken(true) after changes.
- Claims are for access control, not profile data.
My usual pattern combines both: a claim for a platform-wide role like superAdmin, and membership documents for org access and org roles. If your product truly has one org per user, putting orgId in a claim is fine and makes rules faster and cheaper.
Why do my queries fail even when the rules look right?
Because Firestore rules are not filters: a query must include the same tenant condition the rules check, or Firestore rejects the entire query.
Firestore evaluates a query against its possible results, not the documents it would actually return. If your rule says a project is readable only when resource.data.tenantId matches the user's org, a query for all projects fails with "Missing or insufficient permissions," even if every project in the database happens to belong to that org. Add where tenantId equals the active org ID and the same query succeeds. With Option A the path does this for you, which is one more reason I prefer it.
This is also the most common symptom when someone adds tenancy to an existing app. I wrote a separate guide, "Firestore "Missing or insufficient permissions": what triggers it and how to fix it," that walks through the other causes. Use the Rules Playground in the Firebase console to test a read as a specific user in a specific org before you deploy.
Do I need Identity Platform multi-tenancy?
Only if each customer needs its own separate user pool or identity provider; most SaaS MVPs should use one user pool and record org membership in Firestore.
Google Cloud Identity Platform supports multi-tenancy at the authentication layer, where each tenant has its own identity providers, settings and set of users, managed from the parent project with the Admin SDK tenant manager. That is useful when an enterprise customer wants their own SAML single sign-on and their own isolated accounts. It is not the same as data isolation: you still need Firestore rules that keep tenants apart. For an early product it adds setup and a sign-in flow that has to pick a tenant first, so I add it when a contract asks for it, not before.
Worked example: a field team tool with three roles
Here is an illustration with a made-up product: CrewBoard, a scheduling tool sold to small landscaping companies. Each company is a tenant with owners, dispatchers and crew members.
- Layout: orgs/{orgId} holds the company name and plan. orgs/{orgId}/members/{uid} holds role and display name. orgs/{orgId}/jobs/{jobId} holds jobs, each with an assignedTo list of user IDs.
- Rules: any member can read jobs in their org. Dispatchers and owners can create and edit jobs. Crew members can update only the status field of jobs assigned to them, checked by comparing request.resource.data against resource.data so no other field changes.
- Claims: a single superAdmin claim for the founder's support account. Org access stays in membership documents, because one landscaper works for two companies.
- Server code: a Cloud Function accepts invites and creates membership documents. Clients can never write to members.
That design uses at most two document lookups per request, keeps queries scoped by path, and handles the person who works for two companies without a single custom claim per org.
What mistakes leak data between tenants?
The leaks I look for first are open membership writes, missing tenant checks on create, and admin screens that query without rules.
- Letting clients write membership or role documents, so anyone can promote themselves.
- Checking tenantId on read but not validating request.resource.data.tenantId on create, so a user can write into another org.
- Storage buckets left out of the plan. Files need the same tenant path and matching Storage rules.
- An internal admin page built with the client SDK and a temporary "allow read: if true," which ships to production.
- Putting a growing list of org IDs into custom claims until token minting fails at the size limit.
Before launch, run through "Firebase best practices before launch: the security rules checklist" with two test users in two different orgs.
When should I get help, and what does it cost?
Get help before your first paying customer if you are unsure whether rules enforce isolation, because a cross-tenant leak is very hard to recover from.
Tenant isolation is part of every multi-role build I do. My SaaS MVP package from $1,200 over about 21 days covers auth, a dashboard, the database and payments for a focused product, and the Full SaaS Platform from $3,000 over about 45 days is the tier for multiple roles, admin tools and integrations, which is where most multi-tenant B2B products land. If you already have an app and only need the rules reviewed and fixed, a Quick Fix starts at $60 for one issue and Fix Plus Features at $200 covers up to three.
The bottom line
Put each customer under orgs/{orgId}, record membership in a subcollection that only server code can write, and write rules that check membership and role before every read and write. Use custom claims for small, stable facts and remember the 1000 byte limit and the token refresh delay. Make every query carry the tenant condition, because rules are not filters. Decide all of this on day one, test with two users in two orgs, and multi-tenancy on Firestore stays boring, which is exactly what you want.
Questions, answered in one line
Should each customer get their own Firebase project?
Usually not, because one project per tenant multiplies deploys, indexes and billing; it only makes sense when a contract requires physical separation.
Can a user belong to more than one organization in Firestore?
Yes, store a membership document per org and let the user pick an active org, rather than putting every org ID into custom claims.
Why do I get Missing or insufficient permissions on a list query?
Usually because the query does not include the tenant filter the rules require, so Firestore cannot prove every possible result is allowed and rejects it.
Do I need Identity Platform multi-tenancy for a B2B SaaS?
Only if each customer needs its own separate user pool or identity provider, such as their own SAML single sign-on; most MVPs do fine with one user pool and org membership.
Can someone review my Firestore rules for tenant leaks?
Thomas Osayi builds multi-role SaaS platforms with tenant isolation and admin tools from $3,000, and fixes a single security rule problem in an existing app from $60.
Sources
Related articles
- How to scope an MVP: one workflow, three lists and the cuts that matter
- Web app vs mobile app for your MVP: which to build first
- PWA vs native app in 2026: what a web app can do on iPhone now
- How to build a marketplace MVP: solve the chicken-and-egg problem first
SaaS MVP and full-stack web app development
I build SaaS MVPs and full-stack web apps in Next.js, TypeScript and Firebase or Postgres: auth, dashboards, payments, roles and admin tools. Founder of SQWAD.
See the service