[PR #8177] [MERGED] fix(client): broadcast session updates to other tabs on sign-out and user update #7797

Closed
opened 2026-03-13 13:49:29 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8177
Author: @Abhinav-kodes
Created: 2/26/2026
Status: Merged
Merged: 2/27/2026
Merged by: @himself65

Base: canaryHead: fix/broadcast-session-update


📝 Commits (2)

  • acb0944 fix(client): broadcast session updates to other tabs on sign-out and user update
  • 6a7ab33 fix(lint): fix biome formatting and import order

📊 Changes

5 files changed (+89 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/client/config.ts (+11 -1)
📝 packages/better-auth/src/client/proxy.ts (+2 -0)
📝 packages/better-auth/src/client/session-atom.ts (+8 -0)
📝 packages/better-auth/src/client/session-refresh.test.ts (+67 -0)
📝 packages/core/src/types/plugin-client.ts (+1 -0)

📄 Description

Problem

broadcastSessionUpdate() was defined in createSessionRefreshManager and returned but never called. Session mutations (sign-out, user updates) were never broadcast to other open tabs, causing stale useSession() state when refetchOnWindowFocus is disabled or rate-limited.

Fixes #8120

Root Cause

The function was dead code — defined at client/session-refresh.ts:137, exported in
the return object at line 217, but zero call sites existed anywhere in the codebase.

Changes

  • packages/core/src/types/plugin-client.ts — Added optional callback?: (path: string) => void to ClientAtomListener
  • packages/better-auth/src/client/proxy.ts — Call match.callback?.(routePath) inside onSuccess after signal fires
  • packages/better-auth/src/client/session-atom.ts — Capture and expose broadcastSessionUpdate outside onMount
  • packages/better-auth/src/client/config.ts — Wire broadcastSessionUpdate into atomListeners callback for /sign-out, /update-user, /update-session
  • packages/better-auth/src/client/session-refresh.test.ts — 2 new tests verifying broadcast fires for signout and updateUser triggers

Testing

All 13 tests pass including 2 new ones.


Summary by cubic

Broadcast session changes to other tabs on sign-out, user update, and session update. This keeps useSession() in sync across tabs and prevents stale state when refetchOnWindowFocus is off or rate-limited.

  • Bug Fixes

    • Expose broadcastSessionUpdate from getSessionAtom and wire it to atomListeners for /sign-out, /update-user, and /update-session.
    • Add optional ClientAtomListener.callback and invoke it from proxy onSuccess to trigger broadcasts.
    • Add tests confirming broadcasts fire and post to the channel for signout and updateUser.
  • Refactors

    • Fix Biome formatting and import order.

Written for commit 6a7ab330a8. 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/8177 **Author:** [@Abhinav-kodes](https://github.com/Abhinav-kodes) **Created:** 2/26/2026 **Status:** ✅ Merged **Merged:** 2/27/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/broadcast-session-update` --- ### 📝 Commits (2) - [`acb0944`](https://github.com/better-auth/better-auth/commit/acb0944a13956b60242971d01251cd5c0d0b6ad7) fix(client): broadcast session updates to other tabs on sign-out and user update - [`6a7ab33`](https://github.com/better-auth/better-auth/commit/6a7ab330a8f1c465cc0d1aa7dc1a2b81854e4b4d) fix(lint): fix biome formatting and import order ### 📊 Changes **5 files changed** (+89 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/client/config.ts` (+11 -1) 📝 `packages/better-auth/src/client/proxy.ts` (+2 -0) 📝 `packages/better-auth/src/client/session-atom.ts` (+8 -0) 📝 `packages/better-auth/src/client/session-refresh.test.ts` (+67 -0) 📝 `packages/core/src/types/plugin-client.ts` (+1 -0) </details> ### 📄 Description ## Problem `broadcastSessionUpdate()` was defined in `createSessionRefreshManager` and returned but never called. Session mutations (sign-out, user updates) were never broadcast to other open tabs, causing stale `useSession()` state when `refetchOnWindowFocus` is disabled or rate-limited. Fixes #8120 ## Root Cause The function was dead code — defined at `client/session-refresh.ts:137`, exported in the return object at line 217, but zero call sites existed anywhere in the codebase. ## Changes - `packages/core/src/types/plugin-client.ts` — Added optional `callback?: (path: string) => void` to `ClientAtomListener` - `packages/better-auth/src/client/proxy.ts` — Call `match.callback?.(routePath)` inside `onSuccess` after signal fires - `packages/better-auth/src/client/session-atom.ts` — Capture and expose `broadcastSessionUpdate` outside `onMount` - `packages/better-auth/src/client/config.ts` — Wire `broadcastSessionUpdate` into `atomListeners` callback for `/sign-out`, `/update-user`, `/update-session` - `packages/better-auth/src/client/session-refresh.test.ts` — 2 new tests verifying broadcast fires for `signout` and `updateUser` triggers ## Testing All 13 tests pass including 2 new ones. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Broadcast session changes to other tabs on sign-out, user update, and session update. This keeps useSession() in sync across tabs and prevents stale state when refetchOnWindowFocus is off or rate-limited. - **Bug Fixes** - Expose broadcastSessionUpdate from getSessionAtom and wire it to atomListeners for /sign-out, /update-user, and /update-session. - Add optional ClientAtomListener.callback and invoke it from proxy onSuccess to trigger broadcasts. - Add tests confirming broadcasts fire and post to the channel for signout and updateUser. - **Refactors** - Fix Biome formatting and import order. <sup>Written for commit 6a7ab330a8f1c465cc0d1aa7dc1a2b81854e4b4d. 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:49:29 -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#7797