SSO: Support multiple domains per provider (enterprise multi-domain SSO) #2699

Closed
opened 2026-03-13 10:13:56 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @ManuelCortes23 on GitHub (Jan 13, 2026).

Originally assigned to: @Paola3stefania on GitHub.

Problem

We're using Better Auth's SSO plugin for enterprise SAML authentication where a single IdP (e.g., Okta) serves multiple email domains belonging to the same organization. For example, a company might have:

  • company.com
  • subsidiary.com
  • acquired-company.com

All three domains authenticate through the same Okta instance with a single ACS URL.

Current Behavior

The ssoProvider schema expects one domain per provider, with providerId marked as unique.

We tried creating multiple ssoProvider rows with the same providerId but different domain values. This breaks because the SAML callback uses findOne by providerId only:

// @better-auth/sso/dist/index.mjs ~line 1802
provider = await ctx.context.adapter.findOne({
  model: "ssoProvider",
  where: [{ field: "providerId", value: providerId }]
})

With multiple rows sharing the same providerId, findOne returns an arbitrary row (e.g., company.com). Then domain validation fails for users from other domains:

// ~line 1974
const isTrustedProvider = ... || validateEmailDomain(userInfo.email, provider.domain);
// validateEmailDomain("user@subsidiary.com", "company.com") → false

Result: account_not_linked error for users from non-primary domains.

Expected Behavior

Enterprise SSO should support multiple email domains per provider, since this is a common real-world configuration where:

  • One IdP instance serves multiple corporate domains
  • All domains share the same SAML configuration and ACS URL
  • Users from any configured domain should authenticate successfully

Current Workaround

Adding the provider to trustedProviders bypasses domain validation entirely, but this removes an important security check.

  • #6481 - SAML account linking requires trustedProviders (related behavior)
  • #6551 - Fix for SAML trusted provider check
  • #2296 - Multiple domains configuration (different issue - about trustedOrigins)

Environment

  • better-auth: 1.4.10
  • @better-auth/sso: 1.4.10
Originally created by @ManuelCortes23 on GitHub (Jan 13, 2026). Originally assigned to: @Paola3stefania on GitHub. ## Problem We're using Better Auth's SSO plugin for enterprise SAML authentication where a single IdP (e.g., Okta) serves multiple email domains belonging to the same organization. For example, a company might have: - `company.com` - `subsidiary.com` - `acquired-company.com` All three domains authenticate through the same Okta instance with a single ACS URL. ## Current Behavior The `ssoProvider` schema expects one domain per provider, with `providerId` marked as unique. We tried creating multiple `ssoProvider` rows with the same `providerId` but different `domain` values. This breaks because the SAML callback uses `findOne` by `providerId` only: ```javascript // @better-auth/sso/dist/index.mjs ~line 1802 provider = await ctx.context.adapter.findOne({ model: "ssoProvider", where: [{ field: "providerId", value: providerId }] }) ``` With multiple rows sharing the same `providerId`, `findOne` returns an arbitrary row (e.g., `company.com`). Then domain validation fails for users from other domains: ```javascript // ~line 1974 const isTrustedProvider = ... || validateEmailDomain(userInfo.email, provider.domain); // validateEmailDomain("user@subsidiary.com", "company.com") → false ``` Result: `account_not_linked` error for users from non-primary domains. ## Expected Behavior Enterprise SSO should support multiple email domains per provider, since this is a common real-world configuration where: - One IdP instance serves multiple corporate domains - All domains share the same SAML configuration and ACS URL - Users from any configured domain should authenticate successfully ## Current Workaround Adding the provider to `trustedProviders` bypasses domain validation entirely, but this removes an important security check. ## Related Issues/PRs - #6481 - SAML account linking requires trustedProviders (related behavior) - #6551 - Fix for SAML trusted provider check - #2296 - Multiple domains configuration (different issue - about trustedOrigins) ## Environment - `better-auth`: 1.4.10 - `@better-auth/sso`: 1.4.10
GiteaMirror added the enhancement label 2026-03-13 10:13:56 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 13, 2026):

It looks like you're proposing a feature request for Better Auth to support multiple email domains under a single SSO provider. This is a common setup in enterprises, and I can see how the current schema and limitations would complicate this.

Your proposed solutions are insightful, suggesting the addition of a domains array to the ssoProvider schema or modifying the query for providerId to also check the domain. Both approaches could effectively address the current limitation.

It's great that you expressed willingness to contribute a PR if there's alignment on the approach! This openness to collaboration is valuable.

You might want to watch for feedback or changes regarding your feature request in the corresponding issue, as domain verification and support for multiple domains is crucial for enterprise users. If you'd like to discuss the next steps or need advice on specific implementations, feel free to ask!

What other enhancements or features are you considering for Better Auth?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 13, 2026): It looks like you're proposing a feature request for Better Auth to support multiple email domains under a single SSO provider. This is a common setup in enterprises, and I can see how the current schema and limitations would complicate this. Your proposed solutions are insightful, suggesting the addition of a `domains` array to the `ssoProvider` schema or modifying the query for `providerId` to also check the domain. Both approaches could effectively address the current limitation. It's great that you expressed willingness to contribute a PR if there's alignment on the approach! This openness to collaboration is valuable. You might want to watch for feedback or changes regarding your feature request in the corresponding issue, as domain verification and support for multiple domains is crucial for enterprise users. If you'd like to discuss the next steps or need advice on specific implementations, feel free to ask! What other enhancements or features are you considering for Better Auth? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@ulic75 commented on GitHub (Jan 18, 2026):

I admit that I might be out of touch as I've not worked with better-auth in a while. However it looks like the authClient.signIn.sso() method allows for specifying the domain. Could build some logic or a db call to do some kind of domain mapping so they would all sign-in using a single domain provider?

https://www.better-auth.com/docs/plugins/sso#sign-in-with-sso

@ulic75 commented on GitHub (Jan 18, 2026): I admit that I might be out of touch as I've not worked with better-auth in a while. However it looks like the `authClient.signIn.sso()` method allows for specifying the domain. Could build some logic or a db call to do some kind of domain mapping so they would all sign-in using a single domain provider? https://www.better-auth.com/docs/plugins/sso#sign-in-with-sso
Author
Owner

@Paola3stefania commented on GitHub (Jan 22, 2026):

Into this @ManuelCortes23

@Paola3stefania commented on GitHub (Jan 22, 2026): Into this @ManuelCortes23
Author
Owner

@Paola3stefania commented on GitHub (Jan 22, 2026):

closing this issue as already implemented :) if you find any issue lets us know. thank you for your collab, to make every day better auth even better @ManuelCortes23

@Paola3stefania commented on GitHub (Jan 22, 2026): closing this issue as already implemented :) if you find any issue lets us know. thank you for your collab, to make every day better auth even better @ManuelCortes23
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2699