[PR #2767] [CLOSED] [POC] Add initial data support to React useSession hook #30077

Closed
opened 2026-04-17 21:16:01 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/2767
Author: @ludwigbacklund
Created: 5/23/2025
Status: Closed

Base: mainHead: use-session-initial-data


📝 Commits (2)

  • bfa558c Implement proof-of-concept passing of initital session data to React useSession
  • 7d5e359 Lint

📊 Changes

5 files changed (+184 additions, -27 deletions)

View changed files

📝 packages/better-auth/src/client/config.ts (+5 -2)
📝 packages/better-auth/src/client/query.ts (+7 -3)
📝 packages/better-auth/src/client/react/index.ts (+35 -15)
packages/better-auth/src/client/react/initial-data-demo.test.ts (+124 -0)
📝 packages/better-auth/src/client/session-atom.ts (+13 -7)

📄 Description

Related to https://github.com/better-auth/better-auth/issues/2768

Summary

Proof of concept for allowing the React useSession hook to accept initial session data, enabling SSR scenarios where session data is already available on the client.

Changes

  • New overload: useSession(initialData) alongside existing useSession()
  • Skip initial fetch: When initial data provided, isPending starts as false and no network request is made
  • Backward compatible: Existing useSession() usage unchanged
  • Type safe: Full TypeScript support with function overloads

Usage

// Normal usage (fetches on mount)
const { data, isPending } = useSession(); // isPending starts true

// With initial data (skips fetch)
const initialSession = { user: {...}, session: {...} };
const { data, isPending } = useSession(initialSession); // isPending starts false

Implementation Details

  • Modified useAuthQuery to accept initialData parameter
  • Enhanced session atom creation to pass through initial data
  • Added React-specific overloads in createAuthClient
  • Maintains refetch functionality in both scenarios

Use Cases

  • SSR with pre-loaded session data
  • Hydration without fetch waterfalls
  • Testing with mock session data

Note: This is a proof of concept to validate the approach and gather feedback before potential refinement.


🔄 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/2767 **Author:** [@ludwigbacklund](https://github.com/ludwigbacklund) **Created:** 5/23/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `use-session-initial-data` --- ### 📝 Commits (2) - [`bfa558c`](https://github.com/better-auth/better-auth/commit/bfa558c578c7be0972dde02ba04bf1dab48c9b70) Implement proof-of-concept passing of initital session data to React useSession - [`7d5e359`](https://github.com/better-auth/better-auth/commit/7d5e359fbc82f0016d8cb2b6351c7564ef07df1f) Lint ### 📊 Changes **5 files changed** (+184 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/client/config.ts` (+5 -2) 📝 `packages/better-auth/src/client/query.ts` (+7 -3) 📝 `packages/better-auth/src/client/react/index.ts` (+35 -15) ➕ `packages/better-auth/src/client/react/initial-data-demo.test.ts` (+124 -0) 📝 `packages/better-auth/src/client/session-atom.ts` (+13 -7) </details> ### 📄 Description Related to https://github.com/better-auth/better-auth/issues/2768 ### Summary Proof of concept for allowing the React `useSession` hook to accept initial session data, enabling SSR scenarios where session data is already available on the client. ### Changes - **New overload**: `useSession(initialData)` alongside existing `useSession()` - **Skip initial fetch**: When initial data provided, `isPending` starts as `false` and no network request is made - **Backward compatible**: Existing `useSession()` usage unchanged - **Type safe**: Full TypeScript support with function overloads ### Usage ```tsx // Normal usage (fetches on mount) const { data, isPending } = useSession(); // isPending starts true // With initial data (skips fetch) const initialSession = { user: {...}, session: {...} }; const { data, isPending } = useSession(initialSession); // isPending starts false ``` ### Implementation Details - Modified `useAuthQuery` to accept `initialData` parameter - Enhanced session atom creation to pass through initial data - Added React-specific overloads in `createAuthClient` - Maintains refetch functionality in both scenarios ### Use Cases - SSR with pre-loaded session data - Hydration without fetch waterfalls - Testing with mock session data **Note**: This is a proof of concept to validate the approach and gather feedback before potential refinement. --- <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-17 21:16:02 -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#30077