# B2B SaaS (organizations / multi-tenant): Next.js + Postgres + Better Auth
URL: https://wwadd.dev/stacks/b2b-saas-nextjs-postgres-better-auth
Last verified: 2026-09-28

Organizations / multi-tenant apps where your customers are companies: one shared database, per-org data, roles, and SSO.

## When to use it
- Customers are companies with several users each, not individuals
- You need invites, roles, and one customer's data kept strictly apart from another's
- Enterprise buyers ask for SSO (SAML or OIDC) and an audit trail
- A small team maintains one codebase for the marketing site and the app

## How the pieces fit
Every table a customer owns carries an organization ID in one shared Postgres database, and every tenant query goes through a single scoped data-access layer — that layer, not scattered filters, is what keeps customers apart. Better Auth's organization plugin supplies organizations, members, invitations, roles, and teams; its SSO plugin adds SAML 2.0 or OIDC sign-in per organization. Next.js runs the membership and role check on the server before any query, and Prisma keeps those queries typed. Writing audit-log rows in the same transaction as the change gives enterprise customers their trail. Postgres Row Level Security is an optional backstop behind the data layer, not a replacement for it.

## The tech and its role
- [Next.js (App Router)](https://wwadd.dev/tools/nextjs): Marketing pages, app UI, server-side authorization
- [Postgres](https://wwadd.dev/tools/postgresql): Shared database; organization ID on every tenant-owned row
- [Prisma](https://wwadd.dev/tools/prisma): Typed queries inside a scoped data-access layer
- [Better Auth](https://wwadd.dev/tools/better-auth): Sessions, organizations, roles, invites, SAML/OIDC SSO
- [Row Level Security (optional)](https://wwadd.dev/tools/postgresql): Database-level backstop behind the data layer
- [Vercel](https://wwadd.dev/tools/vercel): Hosting and preview deployments

## Real-world examples
- Formbricks: Open-source survey platform: a Next.js app on Postgres with Prisma, Organization and Membership models, Better Auth for sign-in, and SAML SSO through BoxyHQ's SAML Jackson (now Ory Polis). (source: https://github.com/formbricks/formbricks)
- Cal.com: Open-source scheduling platform: a Next.js app on Postgres with Prisma, organizations built on its Team model, and enterprise SAML through SAML Jackson. (source: https://github.com/calcom/cal.com)

## Skip if
Skip if your customers are individuals, not organizations — per-tenant scoping, roles, and SSO add weight a single-user app never uses.

## Testing and shipping
- [Test the behavior users depend on](https://wwadd.dev/moves/testing-shipping/test-user-behavior)
- [Check keyboard access and automated accessibility](https://wwadd.dev/moves/testing-shipping/keyboard-accessibility-check)
- [Investigate flaky browser tests](https://wwadd.dev/moves/testing-shipping/diagnose-flaky-tests)
- [Verify the production build before release](https://wwadd.dev/moves/testing-shipping/verify-built-release)
- [Keep secrets out of browser bundles](https://wwadd.dev/moves/testing-shipping/keep-secrets-server-side)
- [Make CI validate the same release inputs](https://wwadd.dev/moves/testing-shipping/ci-release-gates)
- [Capture errors with useful context](https://wwadd.dev/moves/testing-shipping/error-reporting-context)
- [Change database schemas in compatible stages](https://wwadd.dev/moves/testing-shipping/compatible-schema-migrations)
- [Prove a backup can restore](https://wwadd.dev/moves/testing-shipping/verify-backup-restore)
- [Prepare rollback before publishing](https://wwadd.dev/moves/testing-shipping/plan-release-rollback)

