Prompt
We already have custom username/password auth. How can I add enterprise SSO in a way that won't force a rewrite or create duplicate accounts?
Latest observation
You can add enterprise SSO without rewriting your auth system by treating SSO as another way to prove identity for the same internal user account.
The key design principle is:
Your app should have one canonical user record per person, and multiple login methods can be linked to that user.
Recommended approach
1) Keep your existing username/password auth as one auth method
Don’t replace it. Add SSO as an additional identity provider:
- local password login
- SAML SSO
- OIDC / OpenID Connect SSO
2) Introduce an internal user ID as the source of truth
Your database should have a stable internal user ID, e.g.:
users.id
Then create a separate table for external identities, e.g.:
user_identitiesuser_idprovider(saml,oidc,google,azuread, etc.)issuersubject(the IdP’s immutable user identifier)email(optional snapshot)tenant/org id(if relevant)- timestamps
This avoids using email as the primary key, which is how duplicate accounts often happen.
How login should work
For SSO users
- User clicks “Sign in with Company SSO”
- Your app redirects to the enterprise IdP
- IdP returns an assertion/token with a stable identifier:
- OIDC:
iss+sub - SAML: NameID or another immutable IdP identifier
- OIDC:
- Your auth service looks up that identity in
user_identities - If found, log in the linked
user_id - If not found, either:
- create a new user automatically, or
- ask the user to match/link an existing account
For existing password users
They continue logging in normally. Later, you can let them connect SSO to their existing account.
Preventing duplicate accounts
Duplicates usually happen when systems match only on email. Avoid that by using a linking flow.
Best practice linking strategy
When an SSO login comes in and no linked identity exists:
- If the email matches an existing verified local account, do not auto-merge blindly
- Require one of:
- the user to log in with their password once and confirm linking
- an admin-approved account linking flow
- a magic link / email verification challenge
- domain-based auto-provisioning only if your business rules allow it
This prevents accidentally merging two different people who share or reuse email addresses across systems.
Enterprise-friendly onboarding patterns
Option A: Just-in-time provisioning
If the SSO identity is new:
- create a user record automatically on first login
- link SSO identity immediately
Good for low-friction enterprise onboarding.
Option B: Invite/link first
If you already have users in your system:
- admin invites enterprise users
- user logs in with password or existing account
- they connect SSO in account settings
Good for reducing accidental duplicates.
Option C: Domain-based auto-linking
If email domain matches the enterprise domain:
- auto-route to SSO
- auto-provision or auto-link only if the user’s email is verified and your policy allows it
Useful, but be careful with shared mailboxes and email changes.
Data model example
users
- id
- email
- name
- status
- created_at
user_identities
- id
- user_id
- provider
- issuer
- subject
- email
- created_at
- last_login_at
Optional:
password_credentialsmfa_methodsorganization_membershipssso_connectionsfor enterprise tenant config
Support both user-owned and org-owned auth
If you sell to enterprises, users may have:
- personal login credentials
- one or more enterprise SSO identities
- membership in one or more organizations
A good pattern is:
user= person/accountorganization= company tenantmembership= user belongs to orgidentity= how the user authenticates
This makes it possible for one person to have:
- password login for personal access
- SSO for Company A
- SSO for Company B
Implementation details to get right
Use immutable identifiers
Do not key identities on:
- email alone
- display name
- mutable username
Use:
- OIDC:
issuer + subject - SAML: IdP entity ID + NameID / stable attribute
Treat email as an attribute, not identity
Emails can change. IdP claims may differ depending on configuration. Use email only for display, notification, and matching assistance.
Store verification status
Track whether:
- the email is verified by the IdP
- the identity has been linked by the user
- the org has approved the connection
Allow account linking and unlinking
Users should be able to:
- add SSO to an existing account
- remove it later if policy allows
- keep password login as fallback if desired
If you need both SAML and OIDC
For enterprise, many teams support:
- OIDC if the IdP supports it well
- SAML for broader enterprise compatibility
You can normalize both into the same internal identity table and login pipeline.
Migration path with minimal disruption
- Add
user_identities - Add SSO provider config per org
- Implement SSO login callback
- Match by immutable external ID first
- Add account linking flow
- Optionally add JIT provisioning
- Let existing password users link SSO later
- Keep password auth as fallback unless enterprise policy disables it
Rule of thumb
Never create a new user just because a new login method appears. First decide whether the incoming identity should:
- map to an existing user
- be linked after verification
- or create a new user intentionally
That’s what prevents rewrites and duplicate accounts.
If you want, I can sketch:
- a database schema,
- the login/linking flow,
- or an example implementation for OIDC/SAML in your stack.