Multi-tenant SaaS tool with multiple apps as tenants

Hello everyone. I am building a no-code tool to build custom apps. Each app can have multiple users (email/pass based login). Each app is hosted on its own subdomain app1.domain1.com, app2.domain2.com, and so on. Essentially, in my case, every custom app is a tenant. An email address will be 2 different users segregated by the app_id. Is such a solution possible via better-auth? Background Research I did some research on multi-tenancy and could only figure out some kind of a hack where the slug of the organization could be the app_id. Essentially organization will be treated as an app. The organization terminology felt a little rigid but I want to see if I can make it work for my use case. There is also an open issue on Github with a similar requirement (https://github.com/better-auth/better-auth/issues/1248), but with no solutions.
GitHub
Issues · better-auth/better-auth
The most comprehensive authentication framework for TypeScript - Issues · better-auth/better-auth
5 Replies
bekacru
bekacru2mo ago
commented on the issue as well you could differentiate emails is by adding the app id to the email address itself. For example, if the email the user is trying to sign up with is [email protected], you can use [email protected] instead. Then, you can use hooks and a util function to handle conversion.
jatin
jatinOP2mo ago
Thank you for the response @bekacru. Appreciate it. Another quick question. For my users using the app on multiple domains, will I have to add all of them to trustedOrigins for CSRF check to proceed? If that's the case, then there could 100s if not 1000s of customer domains that'll need to be dynamically added to trustedOrigins.
bekacru
bekacru2mo ago
you can pass a function instead of a list of origins
jatin
jatinOP2mo ago
Thank you once again. Appreciate the prompt support @bekacru.
lozaa25
lozaa254w ago
Thank you for your approach! But I was wondering if it would be feasible to create a new Postgres schema per tenant and use it exclusively for auth? Then, use a separate better-auth instance for each tenant.
Some potential issues with my approach:
- Too many database connections - No way to retrieve all users - Kysely's support for schemas - Feels hacky
What do you think?
PostgreSQL Documentation
5.10. Schemas
5.10. Schemas # 5.10.1. Creating a Schema 5.10.2. The Public Schema 5.10.3. The Schema Search Path 5.10.4. Schemas and Privileges 5.10.5. …

Did you find this page helpful?