Prompt
How do I build an account linking flow for email and Google login?
Latest observation
A solid account-linking flow for email/password + Google login should do two things well:
- Prevent duplicate accounts for the same person.
- Avoid accidentally merging two different people’s accounts.
Here’s a practical way to design it.
1) Decide your account model
Use one user record and multiple identities/auth methods attached to it.
Example tables:
-
users
- id
- name
- created_at
- status
-
auth_identities
- id
- user_id
- provider (
email,google) - provider_subject (
google sub, or email login identifier) - email_verified
- created_at
This lets one user have:
- password/email login
- Google login
- later, maybe Apple, GitHub, etc.
2) Define your matching rules
Safe automatic linking
Only auto-link when you have strong proof it’s the same person.
Good candidates:
- The user is already signed in and explicitly connects Google from settings.
- Google returns a verified email that exactly matches the signed-in account’s email.
Unsafe automatic linking
Avoid auto-linking just because:
- the email strings match but email is unverified
- the provider returns a name matching an existing user
- the user is not signed in and you only see the same email
3) Recommended user flows
Flow A: New user signs up with email
- User enters email/password.
- Create
usersrow. - Create
auth_identitiesrow foremail. - Send verification email.
- Mark email verified once confirmed.
Flow B: New user continues with Google
- User clicks “Continue with Google”.
- OAuth completes.
- Check Google
sub:- If already linked, log them in.
- If not linked, check verified email against existing users.
- If no match, create new user + Google identity.
- If a match exists, decide whether to:
- auto-link, or
- ask the user to sign in first and then link
4) Best practice for linking email and Google
Preferred UX
If a user is already logged in and wants to add Google:
- Go to Account Settings → Connected Accounts
- Click Link Google
- Complete Google OAuth
- If Google email matches the current account email and is verified, attach the identity
This is the cleanest approach.
If user starts with Google, then later email/password
- Create account from Google first
- Later allow them to set a password
- Or allow them to add email/password as a second credential
5) Handle the “same email” case carefully
Suppose a user signs in with Google and Google says:
- email =
alice@example.com - email_verified = true
And you already have a user with email alice@example.com.
You have two reasonable options:
Option 1: Auto-link if email is verified
Pros:
- smooth UX Cons:
- if your system allows unverified email accounts or multiple people share access to inbox, there’s some risk
Option 2: Require the user to prove ownership first
Example:
- “We found an account with this email. Please sign in with your existing method to link Google.” Pros:
- safer Cons:
- slightly more friction
For most apps, Option 2 is safest unless your verification is strong and your risk is low.
6) Avoid account takeover
Never do this:
- User logs in with Google
- Google email matches an existing user email
- You instantly attach Google to that account without verification
This can be risky if:
- the existing account email isn’t verified
- someone else controls that Google account
- emails were reused/shared
Safer pattern:
- If already logged in, allow linking.
- If not logged in, treat the Google identity as a separate login unless you can verify ownership via email confirmation or a recovery flow.
7) Suggested login/linking decision tree
When Google OAuth succeeds:
-
Find identity by provider + subject
- If found → log into linked user
-
Else if user is currently logged in:
- Link Google identity to current user
- If Google email conflicts with current user email, warn and require confirmation
-
Else if Google email matches an existing user and email is verified:
- Show “We found an existing account. Sign in to link it.”
- Ask for password/email login or another proof step
-
Else:
- Create new user + Google identity
8) What to store for Google
Store:
provider = googleprovider_subject = google_user_id(subclaim from ID token)emailemail_verifiedaccess_token/refresh_tokenonly if you need them to call Google APIs
Important:
- Use Google
subas the stable external ID, not email. - Email can change;
subis the real identity.
9) What to store for email login
For email/password:
- store password hash
- store email verification status
- optionally create an
auth_identityrow foremail
If you allow “magic link” email login instead of password, that can also be an identity provider of its own.
10) Example pseudocode
async function handleGoogleLogin(googleProfile, currentUser = null) {
const { sub, email, email_verified } = googleProfile;
const existingIdentity = await db.auth_identities.findOne({
provider: 'google',
provider_subject: sub
});
if (existingIdentity) {
return login(existingIdentity.user_id);
}
if (currentUser) {
await db.auth_identities.insert({
user_id: currentUser.id,
provider: 'google',
provider_subject: sub,
email,
email_verified
});
return login(currentUser.id);
}
const existingUser = await db.users.findOne({ email });
if (existingUser && email_verified) {
// safer: prompt user to sign in first or confirm link
return {
action: 'link_required',
message: 'An account already exists with this email. Please sign in to link Google.'
};
}
const user = await db.users.insert({ email });
await db.auth_identities.insert({
user_id: user.id,
provider: 'google',
provider_subject: sub,
email,
email_verified
});
return login(user.id);
}
11) Recommended UX copy
Helpful messages:
- “Continue with Google”
- “Link Google account”
- “We found an account with this email. Please sign in to connect Google.”
- “This Google account is already linked to another profile.”
12) Edge cases to plan for
- User changes email on Google
- User deletes one login method and still needs another
- User has multiple Google accounts
- Existing account uses unverified email
- Duplicate accounts already exist and need merge
- Refresh token revocation
- Account recovery if one provider is lost
13) Best practice summary
If you want the safest, simplest pattern:
- Use a single user record with multiple linked identities
- Match Google by
sub, not email - Allow linking only when:
- the user is already authenticated, or
- the user proves ownership of the existing account
- Use email verification before any automatic merge
- Never auto-merge solely based on email string
If you want, I can also give you:
- a database schema,
- a full flowchart, or
- example code for Node.js / Next.js / Django / Firebase / Auth0 / Supabase.