Prompt
How do I add social login without breaking my existing email/password accounts?
Latest observation
To add social login safely, the main rule is:
don’t create a second account just because the login method is different.
Instead, make social login another way to prove ownership of the same user identity.
Here’s the practical approach.
1) Keep one user record, multiple sign-in methods
Your users table should represent the person/account, not the login method.
Add a separate table like:
usersauth_identitiesoruser_logins
Example:
usersidemailnamecreated_at
auth_identitiesiduser_idprovider(email,google,apple,github, etc.)provider_user_idpassword_hashnullablecreated_at
This lets one user have:
- email/password
- Google login
- Apple login
all attached to the same account.
2) Match by verified email, but carefully
When a user signs in with Google/Apple/etc., you may receive an email address.
If that email is:
- verified by the provider
- and already exists in your system
then you can usually attach the new social identity to that existing user.
Important: only do this automatically if the provider gives you a verified email and you trust that provider’s verification.
For providers where email may not be verified or may be hidden, require the user to log in with their existing method first, then link the new provider manually.
3) Support “link account” after login
The safest UX is:
- User logs in with their existing method.
- In account settings, they click “Connect Google” or “Connect Apple”.
- You start the OAuth flow.
- On return, attach that provider identity to the logged-in user.
This avoids accidental account takeover and prevents duplicate accounts.
4) Handle these cases explicitly
Case A: New social user, no existing email
- Create a new user record.
- Create an auth identity for the provider.
Case B: Social login email matches an existing account
If email is verified:
- either sign in to that existing account
- or prompt the user to confirm/link, depending on your risk tolerance
Case C: Social login email matches, but email is unverified or ambiguous
- Don’t auto-merge
- Ask the user to sign in with email/password first, then link the provider
Case D: Existing email/password user adds social login
- Keep password login active unless they explicitly remove it
- Add the provider as an additional sign-in method
5) Never use email alone as the primary identity
Email is useful, but it’s not always a guaranteed unique auth identity because:
- people can change emails
- multiple providers can return the same email
- some providers hide email
- users may have old accounts with the same address pattern
Use a combination of:
- provider name
- provider user ID
- verified email for account matching
6) Let users manage login methods
Add an account settings page showing:
- connected providers
- password status
- option to add/remove providers
- option to set or change password
Be careful:
- don’t allow removing the last login method unless another one exists
- require re-authentication for sensitive changes
7) Migration strategy for existing users
If you already have email/password users:
- do not migrate them into separate social accounts
- introduce the new
auth_identitiestable - create an identity record for existing password users
- then let them add social providers later
Example:
- existing user:
user_id = 42 - add identity:
- provider =
email - password_hash =
...
- provider =
Then add:
- provider =
google - provider_user_id =
abc123
8) Watch out for these common mistakes
- Creating duplicate users when a social login email already exists
- Auto-linking accounts without verified email
- Letting users change email and accidentally hijack another account
- Not storing provider user IDs
- Allowing account linking without confirming the user is already authenticated
9) Recommended safe default flow
If you want the simplest safe implementation:
- Email/password users keep logging in as usual
- Social login can create new accounts
- If a social login email matches an existing account, show:
- “We found an existing account with this email. Please sign in with your password first to link Google.”
- After login, let them connect the provider
That’s the safest path and avoids breaking existing accounts.
If you want, I can also show:
- a database schema for this
- an account-linking flow diagram
- or sample code for a specific stack like Django, Rails, Node, Laravel, or Firebase.