[GH-ISSUE #3814] Duplicated oauth accounts because of type mismatch #9734

Closed
opened 2026-04-13 05:24:53 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @cemreinanc on GitHub (Aug 5, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3814

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Add generic oauth plugin and setup a provider that returns accountId's as only numbers.
  2. Try to login multiple times with that oauth provider.

Current vs. Expected behavior

Expected to have only one account item for the same providerId accountId combination but instead every time user logins, that creates new account in the database with same providerId and accountId.

When I dug deeper, I found that in this section e57a49f99d/packages/better-auth/src/oauth2/link-account.ts (L43-L47) accountId returns as a string from database but other one returns as a number from the endpoint. So they dont align and cause hasBeenLinked to be undefined.

in line 46 comparing like this a.accountId === String(account.accountId) seems like solved the issue.

What version of Better Auth are you using?

1.3.4 or 1.3.5-beta2 both

Provide environment information

(not relevant)
- OS: MacOS
- Browser: Chrome

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)


Additional context

might be related to #2062 too.

Originally created by @cemreinanc on GitHub (Aug 5, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3814 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Add generic oauth plugin and setup a provider that returns accountId's as only numbers. 2. Try to login multiple times with that oauth provider. ### Current vs. Expected behavior Expected to have only one account item for the same providerId accountId combination but instead every time user logins, that creates new account in the database with same providerId and accountId. When I dug deeper, I found that in this section https://github.com/better-auth/better-auth/blob/e57a49f99dbf72d462d94e8f8ccaec747eb1a725/packages/better-auth/src/oauth2/link-account.ts#L43-L47 accountId returns as a string from database but other one returns as a number from the endpoint. So they dont align and cause `hasBeenLinked` to be undefined. in line 46 comparing like this `a.accountId === String(account.accountId)` seems like solved the issue. ### What version of Better Auth are you using? 1.3.4 or 1.3.5-beta2 both ### Provide environment information ```bash (not relevant) - OS: MacOS - Browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context might be related to #2062 too.
GiteaMirror added the locked label 2026-04-13 05:24:54 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 5, 2025):

Hi @cemreinanc! I'm Dosu and I’m helping the better-auth team.

You’ve correctly identified the root cause: the type mismatch between accountId from the OAuth provider (number) and from the database (string) leads to duplicate accounts because strict equality fails when types differ. The codebase expects accountId to be a string everywhere, and casting both sides to string before comparison is the right fix and aligns with the schema and best practices for identifier normalization [code reference][schema reference]. This issue is not unique to your setup and is a general pitfall with providers that return numeric IDs.

There’s no evidence that this fix has shipped in the latest releases—related PRs are either not merged or not directly addressing this comparison. Until an official fix lands, casting accountId to string in the comparison is a safe and robust workaround. This approach is consistent with the codebase’s expectations and won’t introduce side effects, as the database and schema both treat accountId as a string [discussion][schema design].

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

<!-- gh-comment-id:3156591436 --> @dosubot[bot] commented on GitHub (Aug 5, 2025): <!-- Greeting --> Hi @cemreinanc! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You’ve correctly identified the root cause: the type mismatch between accountId from the OAuth provider (number) and from the database (string) leads to duplicate accounts because strict equality fails when types differ. The codebase expects accountId to be a string everywhere, and casting both sides to string before comparison is the right fix and aligns with the schema and best practices for identifier normalization [[code reference](https://github.com/better-auth/better-auth/blob/669ce3e6b737ce7c1a9ce982bf092f00d7373fbc/packages/better-auth/src/oauth2/link-account.ts)][[schema reference](https://github.com/better-auth/better-auth/blob/669ce3e6b737ce7c1a9ce982bf092f00d7373fbc/packages/better-auth/src/db/schema.ts)]. This issue is not unique to your setup and is a general pitfall with providers that return numeric IDs. There’s no evidence that this fix has shipped in the latest releases—related PRs are either not merged or not directly addressing this comparison. Until an official fix lands, casting accountId to string in the comparison is a safe and robust workaround. This approach is consistent with the codebase’s expectations and won’t introduce side effects, as the database and schema both treat accountId as a string [[discussion](https://github.com/better-auth/better-auth/discussions/3558)][[schema design](https://github.com/better-auth/better-auth/issues/3310)]. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/58515ea8-215f-4d75-8a9f-914e7e914ab7?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/58515ea8-215f-4d75-8a9f-914e7e914ab7?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/58515ea8-215f-4d75-8a9f-914e7e914ab7?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/58515ea8-215f-4d75-8a9f-914e7e914ab7?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/58515ea8-215f-4d75-8a9f-914e7e914ab7?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/58515ea8-215f-4d75-8a9f-914e7e914ab7?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/58515ea8-215f-4d75-8a9f-914e7e914ab7?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3814)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9734