๐Ÿ“ฆ leonardomso / tanstack-better-auth-session

โ˜… 0 stars โ‘‚ 0 forks ๐Ÿ‘ 0 watching
๐Ÿ“ฅ Clone https://github.com/leonardomso/tanstack-better-auth-session.git
HTTPS git clone https://github.com/leonardomso/tanstack-better-auth-session.git
SSH git clone git@github.com:leonardomso/tanstack-better-auth-session.git
CLI gh repo clone leonardomso/tanstack-better-auth-session
Leonardo Maldonado Leonardo Maldonado Initial commit: TanStack Start + Better Auth issue reproduction e68444b 2 months ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ public
๐Ÿ“ src
๐Ÿ“„ .env.example
๐Ÿ“„ .gitignore
๐Ÿ“„ biome.json
๐Ÿ“„ bun.lock
๐Ÿ“„ package.json
๐Ÿ“„ postcss.config.mjs
๐Ÿ“„ README.md
๐Ÿ“„ tsconfig.json
๐Ÿ“„ vite.config.ts
๐Ÿ“„ README.md

TanStack Start + Better Auth - Issue Reproduction

A minimal reproduction repository demonstrating an issue with Better Auth session cookies when using the reactStartCookies plugin with session-related plugins in TanStack Start.

Issue

Session token cookie not set when using reactStartCookies plugin with session-related plugins in TanStack Start

The session token cookie is not properly set in the browser when combining the reactStartCookies plugin with other session-related plugins (multiSession, lastLoginMethod, oneTap) and/or enabling session cookie cache.

See auth.ts for the configuration and inline comments describing the issue.

Prerequisites

  • Bun (or Node.js 18+)
  • PostgreSQL database

Setup

  • Clone the repository
git clone <your-repo-url>
   cd tanstack-better-auth-test

  • Install dependencies
bun install
   # or
   npm install

  • Set up environment variables
Copy .env.example to .env and fill in the required values:

cp .env.example .env

Required environment variables:

  • DATABASE_URL: PostgreSQL connection string
  • BETTER_AUTH_SECRET: Secret key for Better Auth (minimum 32 characters)
  • VITE_BASE_URL: Base URL for your app (e.g., http://localhost:3000)
  • GOOGLE_CLIENT_ID: Google OAuth client ID
  • GOOGLE_CLIENT_SECRET: Google OAuth client secret
  • Set up Google OAuth
  • Create a new project or select an existing one
  • Enable Google+ API
  • Create OAuth 2.0 credentials
  • Add authorized redirect URI: http://localhost:3000/api/auth/callback/google
  • Copy the client ID and secret to your .env file
  • Set up the database
Create a PostgreSQL database:

createdb better_auth_test

Apply the database schema using Better Auth CLI:

npx @better-auth/cli migrate
   # or
   npx drizzle-kit push

  • Run the development server
bun run dev
   # or
   npm run dev

The app will be available at http://localhost:3000

Reproducing the Issue

  • Start the development server
  • Navigate to the authentication page
  • Click "Sign in with Google"
  • Complete the Google OAuth flow
  • Open browser DevTools > Application > Cookies
  • Expected: Session token cookie should be present
  • Actual: Session token cookie is missing

Testing Different Configurations

The issue can be reproduced by uncommenting different sections in src/lib/auth.ts:

Configuration 1: All plugins enabled (current state)

  • Lines 30-33: multiSession(), lastLoginMethod(), oneTap(), reactStartCookies()
  • Result: โŒ Session cookie not set
Configuration 2: With session cache
  • Uncomment lines 55-60 (session.cookieCache)
  • Result: โŒ Session cookie not set
Configuration 3: Only reactStartCookies (workaround)
  • Comment out lines 30-32 (keep only reactStartCookies())
  • Result: โœ… Session cookie works

Environment

  • Better Auth: v1.3.33
  • TanStack Start: v1.133.34
  • TanStack React Router: v1.133.32
  • React: v19.2.0
  • Drizzle ORM: v0.44.7
  • PostgreSQL: Latest
  • Runtime: Bun (or Node.js)

Project Structure

src/
โ”œโ”€โ”€ lib/
โ”‚   โ”œโ”€โ”€ auth.ts          # Better Auth configuration (issue reproduction)
โ”‚   โ”œโ”€โ”€ db.ts            # Drizzle database instance
โ”‚   โ””โ”€โ”€ db/
โ”‚       โ””โ”€โ”€ schema.ts    # Database schema (user, session, account, verification)
โ””โ”€โ”€ routes/              # TanStack Start routes

Related Files

Contributing

If you have insights or potential fixes for this issue, please feel free to open a PR or discussion.

License

MIT