Skip to main content

SENIOR SOFTWARE ENGINEER · SYSTEM ARCHITECT

Blog · Security & Identity

Multi-tenant auth without tenant leakage

Handling dynamic OAuth providers, passkeys, and edge JWT verification across isolated customer databases.

Product

Category

4 min read

Reading time

Oct 2026

Published

Security, Bun, Auth

Topics

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 calls bis-auth's edge verification endpoint. Ingress resolves the hostname to a verified X-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

  1. Never trust inbound internal headers: Any X-Tenant-ID, X-User-ID, or X-Platform-Admin header sent by an external client is unconditionally stripped at Caddy ingress before the gateway injects verified values.
  2. 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.
  3. Evict caches synchronously on ownership or role changes: When a project transfers to a new organization or a member is removed, bis-auth evicts member caches and publishes a cluster-wide cache.invalidate event before returning success.
  4. 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.

DIRECT MESSAGE

Hiring for a Senior/Staff Architect role, scaling a venture, or looking to collaborate?

Message sent

Thank you. I reply within 24 hours.

Sending…

hi@nazmulislamfahim.com

Thank you. I reply within 24 hours.