Building for Teams: Implementing Multi-Tenancy and Organizations in Next.js
Karl Gusta
Instructor & Founder
Most successful SaaS products follow a simple trajectory: they start by solving a problem for an individual, but they scale by solving problems for teams. If you want to move from $20/month subscriptions to $500/month enterprise deals, you need to move beyond the "Single User" model and into the world of Multi-Tenancy.
Multi-tenancy allows users to create organizations, invite teammates, and collaborate within a shared workspace. However, this adds a significant layer of complexity to your database and security logic. You are no longer just checking "Is this user logged in?"; you are checking "Does this user belong to Organization A, and do they have permission to edit this specific project?" This lesson will show you how to architect these complex relationships using MongoDB and Next.js.
The Problem: The "Flat" Data Model Trap
Early-stage developers often build their data models with a direct link between a User and a Resource (e.g., User -> Post). This works for a personal blog, but it fails for a business tool.
When you try to add team features to a flat model, you run into:
- Data Leaks: Users accidentally seeing data belonging to another company because a query was missing a filter.
- Billing Complexity: Trying to figure out who to charge when five different users are working in the same workspace.
- Permission Chaos: Hardcoding "Admin" and "Member" roles into every single page, making it impossible to update roles later.
- Onboarding Friction: Forcing every team member to create their own billing profile instead of joining a shared corporate account.
Without a multi-tenant architecture, your SaaS is capped at the "Prosumer" level and will struggle to capture the lucrative B2B market.
The Shift: The Workspace-Centric Model
The secret to scaling and advanced features is shifting the "Owner" of your data from the User to the Organization.
- The Organization Document: A central hub that holds the billing status, the team name, and the list of members.
- The Membership Map: A join-collection or an array that defines the relationship between a User and an Organization, including their specific Role (Owner, Admin, Member).
- Scoped Queries: Every database query in your app must now include an
orgIdfilter. You never fetch "All Projects"; you fetch "All Projects where orgId equals the current active workspace."
This shift ensures that your application is built for collaboration from the ground up.
Deep Dive: Implementing the Team Workflow
To build a professional multi-tenant system, we focus on the Organization Schema, the Workspace Switcher, and Role-Based Access Control (RBAC).
1. The Multi-Tenant Schema
We introduce a new organizations collection in MongoDB. This collection becomes the "Parent" for all other application data.
typescript// models/Organization.ts { _id: ObjectId("..."), name: "Acme Corp", slug: "acme-corp", ownerId: ObjectId("user_123"), members: [ { userId: ObjectId("user_123"), role: "owner" }, { userId: ObjectId("user_456"), role: "member" } ], stripeCustomerId: "cus_...", plan: "enterprise" }
2. Scoping Data to the Organization
When a user creates a project, the project document must store the orgId. When fetching data for the dashboard, we retrieve the "Active Organization" from the user's session or the URL.
typescript// actions/projects.ts export async function getProjects(orgId: string) { const client = await clientPromise; const db = client.db(); // Critical: Always scope by orgId to prevent data leaks return await db.collection('projects') .find({ orgId: new ObjectId(orgId) }) .toArray(); }
3. The Workspace Switcher UI
A professional SaaS allows a user to be a member of multiple organizations (e.g., a freelancer working for three different clients). We use a "Workspace Switcher" in the sidebar to allow users to toggle between their different contexts.
Key Benefits and Learning Outcomes
- Higher Revenue Potential: You can charge per "Seat" or "Member," allowing your revenue to grow automatically as your customers grow their teams.
- Enterprise Readiness: You provide the security and isolation that corporate IT departments require.
- Network Effects: When one user invites five teammates, your user base grows organically without any extra marketing spend.
- Clean Data Isolation: Even if a user belongs to two different companies, their work for Company A never touches the database records of Company B.
Common Mistakes
- Trusting the Client for
orgId: Never allow the frontend to tell the server whichorgIdto update. Always verify on the server that the logged-in user actually has permission to access that specificorgId. - Forgetting to Index: As your database grows, queries like
find({ orgId: ... })will become slow. You must create an index on theorgIdfield for every collection in your app. - Global Searches: Avoid building features that search across all organizations. Keep your logic focused on the current active workspace to maintain performance and security.

Pro Tips and Best Practices
Use URL-Based Routing:
Instead of storing the active workspace in a hidden state, put it in the URL (e.g., /org/acme-corp/dashboard). This allows users to bookmark specific workspaces and share deep links with their teammates.
Implement Invitation Tokens: When inviting a member, don't just add them to the database. Generate a unique, expiring token and send it via Resend. This ensures that only the person with access to that email can join the team.
Shared Billing:
Ensure that if one admin pays for the "Team Plan," everyone in that organization gets access to the features. Use the orgId to check the subscription status in your Stripe logic, rather than the individual userId.
How This Fits Into the Zero to SaaS Journey
Building for teams is the "Graduation" step of a SaaS founder. In the Zero to SaaS curriculum, we move to multi-tenancy once the core single-user features are rock solid.
By implementing an organization-based model, you are setting yourself up for long-term scalability. You aren't just building a tool; you are building a platform where businesses can operate.
Real-World Example: A Project Management Tool
- User A creates an account and an organization called "Design Studio."
- User A invites User B to join "Design Studio" as an "Editor."
- User B logs in and sees all the projects created by User A because they share the same
orgId. - User A upgrades the "Design Studio" account to Pro. User B instantly gets Pro features because the subscription is tied to the organization, not the individual.
This is the exact model used by giants like Slack, Notion, and Figma.

Action Plan and What to Build Next
- Update your User schema to support an array of
organizationIds. - Create the Organizations collection and build a "Create Team" form.
- Refactor your primary data queries to include an
orgIdfilter. - Build a simple Sidebar Switcher to allow users to toggle between teams.
Now that your SaaS supports teams, you are ready to handle advanced workflows like Activity Logs and Auditing. Would you like to learn how to track "who did what" within an organization to provide even more value to your business customers?