baseURL in case of wildcard subdomains?

I wasn't able to find this in the docs, maybe I didn't look in the right place.

I have multi-tenancy with subdomains, and I want the auth paths to be on each tenant subdomain, for ex:
  • acme.myapp.com/auth/login
  • foo.myapp.com/auth/login
Does better auth support this? The baseUrl value is what's tripping me up. I'm using Fastify, so I'm thinking of doing the following:

  • use a preHandler hook, where I get the subdomain, generate the tenant-specific baseUrl, and create a better-auth instance.
  • Decorate the request with the better-auth instance
  • in the /api/auth route, do await req.auth.handler(req);
Is there anything about this that wouldn't work, or be problematic?
Was this page helpful?