Skip to content

fix(auth): Google sign-in for accounts that already exist - #40

Merged
vlidholt merged 1 commit into
mainfrom
fix/google-signin-linked-account
Oct 29, 2026
Merged

vlidholt merged 1 commit into
mainfrom
fix/google-signin-linked-account

Conversation

@vlidholt

Copy link
Copy Markdown
Contributor

Problem

Some users can't sign in with Google: the server answers with an internal error. Production log (googleIdp.loginWithCode):

DatabaseUniqueViolationException: duplicate key value violates unique constraint
"serverpod_auth_profile_user_profile_email_auth_user_id" … table: serverpod_auth_core_profile

The domain doesn't matter. It hits anyone who signs in with Google for the first time after creating an email/password account:

  1. The Google IDP finds no GoogleAccount, so it creates a new auth user.
  2. Our afterGoogleAccountCreated sees the email belongs to an existing account. It moves the Google account to that account and deletes the new auth user.
  3. GoogleIdp.login still sees newAccount: true and calls createUserProfile for the existing auth user, which already has an auth profile from its email sign-up. The unique violation rolls back the whole sign-in.

The package code is the same in 4.1.0-beta.3 and rc.1, so this has been broken since Google sign-in shipped.

Fix

wonkyGoogleIdpConfig() now returns a GoogleIdpConfig subclass whose build gives the IDP a LinkedAccountUserProfiles. Its createUserProfile returns the existing auth profile when there is one. Otherwise it creates the profile as before. Spec 04 §1.6 is updated.

Tests

test/integration/platform/google_idp_test.dart:

  • Linked account: replays afterGoogleAccountCreated and then the IDP's createUserProfile. It fails with the production error when run with the stock UserProfiles.
  • New Google account: the auth profile is still created.

All 4 tests in the file pass.

Not changed

After linking, the issued token takes its scopes from the deleted new auth user (empty) instead of the linked user. Wonky doesn't use Serverpod auth scopes today, so this has no effect yet.

🤖 Generated with Claude Code

A first Google sign-in for an email with an existing Wonky account
failed with a unique violation on serverpod_auth_core_profile:
afterGoogleAccountCreated links the Google account to the existing
auth user, but GoogleIdp.login still treats the account as new and
creates its auth profile, which the existing user already has.

The Google IDP now gets LinkedAccountUserProfiles, which returns the
existing auth profile instead of inserting a second one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vlidholt
vlidholt merged commit 8e5fcd9 into main Oct 29, 2026
41 checks passed
@vlidholt
vlidholt deleted the fix/google-signin-linked-account branch October 29, 2026 14:26
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant