[PR #7331] [MERGED] refactor: return linked account in findOAuthUser #15482

Closed
opened 2026-04-13 10:03:08 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7331
Author: @OscarCornish
Created: 1/13/2026
Status: Merged
Merged: 1/19/2026
Merged by: @himself65

Base: canaryHead: refactor/oauth-linked-account


📝 Commits (4)

  • 7287098 refactor: return linked account in findOAuthUser
  • b1a1066 chore: Updating typing on linkedAccount to use null over undefined
  • 48b70ac Merge branch 'canary' into refactor/oauth-linked-account
  • cde37ab Merge branch 'canary' into refactor/oauth-linked-account

📊 Changes

3 files changed (+35 additions, -24 deletions)

View changed files

📝 packages/better-auth/src/db/internal-adapter.ts (+20 -15)
📝 packages/better-auth/src/oauth2/link-account.ts (+10 -8)
📝 packages/core/src/types/context.ts (+5 -1)

📄 Description

Summary

  • Query for user-account combination with a findOne instead of the slightly less performant & less semantic findMany and find
  • findOauthUser now returns a linkedAccount when it found a match to the given parameters - this allows for the hasBeenLinked logic to be cleaned up (and given a more semantic name)

Key motivation here is that the values for accountId are stored case-insensitively in my database, but in the existing setup the search for the matching accountId is done outside of the db context. If we move it into the db context, then my adapter will handle this for me.

Notes

  • linkedAccount is optional to preserve compatibility for older adapters/mocks, I could be more aggressive with the refactoring if you're happy to break this

Summary by cubic

Refactored OAuth account lookup to use a single, DB-level query and return the matched linked account, improving correctness for case-insensitive accountId matching and simplifying linking logic.

  • Refactors
    • Use findOne with accountId + providerId (with user join) instead of findMany + filter.
    • findOAuthUser now returns linkedAccount; handleOAuthUserInfo uses it for linking, token updates, and cookies.
    • Context type updated to include linkedAccount: Account | null in findOAuthUser return.

Written for commit cde37ab21b. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7331 **Author:** [@OscarCornish](https://github.com/OscarCornish) **Created:** 1/13/2026 **Status:** ✅ Merged **Merged:** 1/19/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `refactor/oauth-linked-account` --- ### 📝 Commits (4) - [`7287098`](https://github.com/better-auth/better-auth/commit/7287098ef56aa2632f4d0239c5efa76b757fc93e) refactor: return linked account in findOAuthUser - [`b1a1066`](https://github.com/better-auth/better-auth/commit/b1a1066ed9faedcb6c938b8f9d1a0aedb5af61f4) chore: Updating typing on linkedAccount to use null over undefined - [`48b70ac`](https://github.com/better-auth/better-auth/commit/48b70acd0b5ac47c2403686152261bea8a02e819) Merge branch 'canary' into refactor/oauth-linked-account - [`cde37ab`](https://github.com/better-auth/better-auth/commit/cde37ab21b094a988aed454a3555a5865cf49776) Merge branch 'canary' into refactor/oauth-linked-account ### 📊 Changes **3 files changed** (+35 additions, -24 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/internal-adapter.ts` (+20 -15) 📝 `packages/better-auth/src/oauth2/link-account.ts` (+10 -8) 📝 `packages/core/src/types/context.ts` (+5 -1) </details> ### 📄 Description ## Summary - Query for user-account combination with a `findOne` instead of the slightly less performant & less semantic `findMany` and `find` - `findOauthUser` now returns a `linkedAccount` when it found a match to the given parameters - this allows for the `hasBeenLinked` logic to be cleaned up (and given a more semantic name) Key motivation here is that the values for accountId are stored case-insensitively in my database, but in the existing setup the search for the matching accountId is done outside of the db context. If we move it into the db context, then my adapter will handle this for me. ## Notes - `linkedAccount` is optional to preserve compatibility for older adapters/mocks, I could be more aggressive with the refactoring if you're happy to break this <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Refactored OAuth account lookup to use a single, DB-level query and return the matched linked account, improving correctness for case-insensitive accountId matching and simplifying linking logic. - **Refactors** - Use findOne with accountId + providerId (with user join) instead of findMany + filter. - findOAuthUser now returns linkedAccount; handleOAuthUserInfo uses it for linking, token updates, and cookies. - Context type updated to include linkedAccount: Account | null in findOAuthUser return. <sup>Written for commit cde37ab21b094a988aed454a3555a5865cf49776. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 10:03:08 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#15482