Skip to content

feat: return 422 email_exists when an unverified provider email is in use - #2945

Open
cemalkilic wants to merge 1 commit into
masterfrom
cemalkilic/auth-2094-return-422-email_exists-and-save
Open

cemalkilic wants to merge 1 commit into
masterfrom
cemalkilic/auth-2094-return-422-email_exists-and-save

Conversation

@cemalkilic

@cemalkilic cemalkilic commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What

An external provider can report an unverified email that already belongs to another user. Auth doesn't link the two accounts, since that would allow an account takeover. Instead, it creates a separate user with no email, commits it, and refuses the login with provider_email_needs_verification. The person ends up with a stray second account.

This PR adds an opt-in flag, GOTRUE_EXPERIMENTAL_REJECT_UNVERIFIED_EMAIL_IN_USE_ENABLED (off by default). With the flag on, Auth refuses this login and saves nothing:

  • It returns 422 email_exists before creating the user. The error rolls back the transaction, so no emailless user is left behind.
  • On the OAuth callback redirect, this maps to error=access_denied&error_code=email_exists. Only this 422 maps that way. Identity linking already returns email_exists as a 400, which keeps mapping to invalid_request.
  • The message is Unverified email with <provider> already belongs to an existing account. Sign in to that account to link <provider>. Clients should branch on error_code.
  • The before-user-created hook is skipped, because no user will be created.

Even with the flag on, today's behavior is kept for:

  • providers in a non-default linking domain (including SSO)
  • providers where email is optional
  • GOTRUE_MAILER_ALLOW_UNVERIFIED_EMAIL_SIGN_INS=true

… use

When an external provider reports an unverified email that already
belongs to another user, Auth creates a separate user with no email.
Behind the new opt-in GOTRUE_EXPERIMENTAL_REJECT_UNVERIFIED_EMAIL_IN_USE_ENABLED
flag, the login is refused with 422 email_exists instead, and nothing is
saved. The before-user-created hook is skipped because no user will be
created. On the OAuth callback redirect this maps to
error=access_denied&error_code=email_exists.

Today's behavior is kept for non-default linking domains, providers with
optional email, and when unverified email sign-ins are allowed.
@cemalkilic
cemalkilic requested a review from a team as a code owner November 20, 2026 10:57

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant