Multi-tenancy with drizzleAdapter

Hi everyone 👋

I'm working on a multi-tenant application where the database (Postgres) is determined based on the current domain or subdomain. I've customized the migration workflow so I can apply the same migrations across multiple schemas or databases — that part is working fine.

Now I'm facing an issue with the drizzleAdapter used in authentication. The current implementation connects to a fixed schema, but in my case, I need it to connect dynamically based on the current tenant.

Is there a way to pass the tenant context (derived from the domain/subdomain) to the auth handler, or customize the authHandler to inject the appropriate schema/db connection?

export const ServerRoute = createServerFileRoute('/api/auth/$').methods({
  GET: ({ request }) => {
    // Get the current tenant and pass it down or update the auth.handler to get the tenant from request
    return auth.handler(request);
  },
  POST: ({ request }) => {
    // Get the current tenant and pass it down or update the auth.handler to get the tenant from request
    return auth.handler(request);
  },
});


Any pointers or examples would be greatly appreciated 🙏
Was this page helpful?