Prompt
account linking OAuth duplicate users
Latest observation
Nov 10, 2026
OpenAI APIWeb search: off
Account linking for OAuth is usually the right fix for duplicate users caused by people signing in with the same email through different providers.
Common cause
If your app treats each OAuth provider login as a separate account, then:
- Google login creates
user_123 - GitHub login with same email creates
user_456
This leads to duplicate profiles, split data, and login confusion.
Recommended approach
1. Use a single internal user record
Keep one canonical users table row for the person, and link multiple identities to it.
Example schema:
users
idemailnamecreated_at
user_identities
iduser_id→ referencesusers.idprovider(google,github, etc.)provider_user_id(the provider’s stable unique subject ID)email_at_link_timecreated_at
Enforce:
- unique
(provider, provider_user_id) - optionally unique email if your product rules require it
2. Prefer provider user ID over email
Do not use email alone as the identity key.
Why:
- emails can change
- some providers may not verify email
- same email can appear across providers
- social providers may return different email states
Use:
providerprovider_user_id/subclaim from OIDC
3. Link by email only with care
If the OAuth provider gives a verified email, you can offer auto-linking or soft matching, but only if:
- the email is verified by the provider
- the existing account is also verified
- you have a secure linking policy
Safer options:
- require the user to be logged into the existing account before linking
- send a confirmation email before merging accounts
- prompt the user if an email match is found
4. Linking flow options
Safe manual linking
- User logs into existing account
- In settings, they click “Link Google”
- OAuth callback links new identity to current user
- Now same user can use either provider
Auto-link with verified email
- User signs in with OAuth
- You find an existing account with same verified email
- Ask the user to confirm linking
- If confirmed, attach identity to existing user
Merge duplicate accounts
If duplicates already exist:
- Choose a canonical account
- Reassign foreign keys/data from duplicate to canonical
- Link identities
- Disable the duplicate account
5. Important security rules
- Never auto-merge accounts solely on unverified email
- Never allow account takeover by linking based only on a guessed email
- Require the user to prove control of the target account when possible
- Log all linking and merging events
6. Handling existing duplicates
To clean up current duplicates:
- identify users with same verified email
- compare linked identities
- detect data conflicts
- merge carefully with audit logs
- keep a mapping table if needed:
merged_from_user_idmerged_into_user_id
7. UI/UX recommendation
Show users:
- “Sign in with Google”
- “Sign in with GitHub”
- “Link another account”
- “You already have an account with this email. Sign in to link it.”
8. Best practice summary
- One person = one internal user record
- Many OAuth identities can point to it
- Use provider subject IDs as primary identity keys
- Link accounts explicitly or via cautious verified-email flow
- Avoid automatic merges without verification
If you want, I can also provide:
- a database schema,
- pseudo-code for linking flow, or
- examples for Auth0, NextAuth, Supabase, Firebase, or Passport.js.