[PR #7769] [CLOSED] feat: add account object to session #7543

Closed
opened 2026-03-13 13:40:58 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7769
Author: @cvienna
Created: 2/3/2026
Status: Closed

Base: canaryHead: feat/add-account-object-to-session


📝 Commits (8)

  • 5a7032b feat(core): add accountId foreign relation to sessionSchema
  • 2553ecd fix(session): made getSession compatible with account object
  • a002b8f fix(types): added account to session type, context & auth
  • 70f6219 fix(adapter) added account to the adapter
  • ec44a65 fix(cookies) added account to cookies
  • decc6b5 fix(OAuth) put account object into the OAuth callback, and in the account linking process
  • ecb745d fix(test types) Fixed type error in test file
  • 12fefd9 Merge branch 'better-auth:canary' into feat/add-account-object-to-session

📊 Changes

10 files changed (+167 additions, -17 deletions)

View changed files

📝 packages/better-auth/src/api/routes/callback.ts (+2 -1)
📝 packages/better-auth/src/api/routes/session.ts (+73 -4)
📝 packages/better-auth/src/cookies/index.ts (+10 -2)
📝 packages/better-auth/src/db/internal-adapter.ts (+34 -6)
📝 packages/better-auth/src/oauth2/link-account.ts (+13 -2)
📝 packages/better-auth/src/types/auth.ts (+2 -1)
📝 packages/better-auth/src/types/types.test.ts (+17 -1)
📝 packages/core/src/db/get-tables.ts (+11 -0)
📝 packages/core/src/db/schema/session.ts (+1 -0)
📝 packages/core/src/types/context.ts (+4 -0)

📄 Description

Note: This is my first ever PR, so I'd really appreciate some feedback, and maybe also make sure to double check my code.

Intro

I've been struggling with multiple accounts lately, I found it a nightmare to deal with multiple accounts per user with allowDifferentEmails set to true. Because there was no way to know which account was currently in use.

What did I do?

I added the account object to the session. So when you call the: getSession() aka /get-session you get following:

{
  account: Account
  session: Session
  user: User
}

Where before you would only get:

{
  session: Session
  user: User
}

Summary by cubic

Adds the account object to session data so clients always know which account is active. getSession now returns { account, session, user } and OAuth sign-ins create sessions tied to the account.

  • New Features

    • getSession returns account alongside session and user.
    • Session cookies/JWT/JWE and cookie cache include account.
    • Internal adapter fetches and returns account with session (single and batch).
    • OAuth flow links/creates the account, returns it, and creates a session with that accountId.
    • Types updated to expose account in inferred Session; tests adjusted.
  • Migration

    • Database: add a non-null accountId field on sessions with a FK to accounts; backfill existing sessions.
    • Ensure all session creation paths provide accountId when creating a session.
    • Update any consumers expecting only { session, user } to handle { account, session, user }.

Written for commit 12fefd946a. 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/7769 **Author:** [@cvienna](https://github.com/cvienna) **Created:** 2/3/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `feat/add-account-object-to-session` --- ### 📝 Commits (8) - [`5a7032b`](https://github.com/better-auth/better-auth/commit/5a7032b6425a59df13ccdde35ce635bda91fbc63) feat(core): add accountId foreign relation to sessionSchema - [`2553ecd`](https://github.com/better-auth/better-auth/commit/2553ecdcbf18cb7d495592c643dc441d01401a3d) fix(session): made getSession compatible with account object - [`a002b8f`](https://github.com/better-auth/better-auth/commit/a002b8f4c8694660056d6c4d76f04667f0e7135c) fix(types): added account to session type, context & auth - [`70f6219`](https://github.com/better-auth/better-auth/commit/70f621921c58690cefa6f3c63c58a34b26464877) fix(adapter) added account to the adapter - [`ec44a65`](https://github.com/better-auth/better-auth/commit/ec44a652547d85f879a7983538f71e8d5035c101) fix(cookies) added account to cookies - [`decc6b5`](https://github.com/better-auth/better-auth/commit/decc6b532104838cf2600d16c7b5d1d1385b38da) fix(OAuth) put account object into the OAuth callback, and in the account linking process - [`ecb745d`](https://github.com/better-auth/better-auth/commit/ecb745d4a2ce64f3e4b8926dd13a53daa9da19d0) fix(test types) Fixed type error in test file - [`12fefd9`](https://github.com/better-auth/better-auth/commit/12fefd946a785c934545e3185768751cc3c0814e) Merge branch 'better-auth:canary' into feat/add-account-object-to-session ### 📊 Changes **10 files changed** (+167 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/callback.ts` (+2 -1) 📝 `packages/better-auth/src/api/routes/session.ts` (+73 -4) 📝 `packages/better-auth/src/cookies/index.ts` (+10 -2) 📝 `packages/better-auth/src/db/internal-adapter.ts` (+34 -6) 📝 `packages/better-auth/src/oauth2/link-account.ts` (+13 -2) 📝 `packages/better-auth/src/types/auth.ts` (+2 -1) 📝 `packages/better-auth/src/types/types.test.ts` (+17 -1) 📝 `packages/core/src/db/get-tables.ts` (+11 -0) 📝 `packages/core/src/db/schema/session.ts` (+1 -0) 📝 `packages/core/src/types/context.ts` (+4 -0) </details> ### 📄 Description Note: This is my first ever PR, so I'd really appreciate some feedback, and maybe also make sure to double check my code. ### Intro I've been struggling with multiple accounts lately, I found it a nightmare to deal with multiple accounts per user with `allowDifferentEmails` set to true. Because there was no way to know which account was currently _in use_. ### What did I do? I added the account object to the session. So when you call the: `getSession()` aka `/get-session` you get following: ```typescript { account: Account session: Session user: User } ``` Where before you would only get: ```typescript { session: Session user: User } ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds the account object to session data so clients always know which account is active. getSession now returns { account, session, user } and OAuth sign-ins create sessions tied to the account. - **New Features** - getSession returns account alongside session and user. - Session cookies/JWT/JWE and cookie cache include account. - Internal adapter fetches and returns account with session (single and batch). - OAuth flow links/creates the account, returns it, and creates a session with that accountId. - Types updated to expose account in inferred Session; tests adjusted. - **Migration** - Database: add a non-null accountId field on sessions with a FK to accounts; backfill existing sessions. - Ensure all session creation paths provide accountId when creating a session. - Update any consumers expecting only { session, user } to handle { account, session, user }. <sup>Written for commit 12fefd946a785c934545e3185768751cc3c0814e. 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-03-13 13:40:58 -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#7543