Summary
Multi-tenant authentication looks simple on a whiteboard until real customers arrive: each project needs its own isolated end-user database, custom domain OAuth callbacks, passkeys, and role permissions—while platform operators still need to switch smoothly across organizations and transfer projects without stale sessions leaking access.
In Yin Business, we solved this by separating edge ingress verification from a dedicated multi-tenant identity service (bis-auth) built on Bun, Elysia, and Better-Auth.
Boundary design: resolve once at ingress, isolate everywhere
Running a separate authentication container per customer does not scale operationally, yet dumping every customer's users into one shared table invites accidental cross-tenant queries. We use a two-tier boundary instead:
- Edge domain & JWT verification (
bis-entry): Caddy terminates TLS for every custom domain and callsbis-auth's edge verification endpoint. Ingress resolves the hostname to a verifiedX-Tenant-ID, strips any client-supplied internal headers, and injects signed identity claims before forwarding to downstream Go services. - Database-level tenant isolation: Each tenant's end users, sessions, passkeys, and OAuth provider credentials live in an isolated database handle. A compromised token or misconfigured query in Project A physically cannot read a user record in Project B.
- Dynamic per-tenant auth policies: Because OAuth client IDs, magic-link senders, and callback URLs are loaded dynamically per tenant configuration, onboarding a new customer domain requires zero redeploys or restarts.
- Fast claim caching with strict invalidation: Verified session claims are cached briefly so protected SSR routes pay sub-millisecond verification overhead, while membership changes immediately broadcast eviction events across the cluster.
Four non-negotiable rules for multi-tenant identity
- Never trust inbound internal headers: Any
X-Tenant-ID,X-User-ID, orX-Platform-Adminheader sent by an external client is unconditionally stripped at Caddy ingress before the gateway injects verified values. - Fail closed on organization and project boundaries: Switching organizations, issuing API keys, or transferring a project between organizations re-verifies active membership and role permissions on every hop—never relying on a cached client-side role claim.
- Evict caches synchronously on ownership or role changes: When a project transfers to a new organization or a member is removed,
bis-authevicts member caches and publishes a cluster-widecache.invalidateevent before returning success. - Separate end-user identity from console operator identity: Customer end users authenticate strictly within their tenant's domain and database; console administrators authenticate against the control plane. Keeping those two session cookies and signing keys completely separate eliminates an entire class of privilege-escalation bugs.
Continue reading
Have an architecture challenge to discuss?
Get in touch for staff-level architecture advisory or product collaboration.