[GH-ISSUE #6995] Bug: OAuth Provider plugin fails with secondaryStorage without clear error #28018

Closed
opened 2026-04-17 19:21:34 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @productdevbook on GitHub (Dec 25, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6995

Description

When using secondaryStorage (e.g., Redis) together with the oauthProvider plugin, the OAuth token exchange fails with a confusing error:

BetterAuthError: Model "session" not found in schema

This error is misleading because the session table does exist in the Drizzle schema - the issue is that Better Auth's internal schema excludes it.

Root Cause

In @better-auth/core/src/db/get-tables.ts, lines 155-158:

//only add session table if it's not stored in secondary storage
...(!options.secondaryStorage || options.session?.storeSessionInDatabase
  ? sessionTable
  : {}),

When secondaryStorage is configured without storeSessionInDatabase: true, the session table is excluded from the internal schema. However, the OAuth Provider plugin requires the session table to function properly during token exchange.

Steps to Reproduce

  1. Configure Better Auth with secondaryStorage (Redis)
  2. Add oauthProvider plugin
  3. Do NOT set session.storeSessionInDatabase: true
  4. Attempt OAuth flow - token exchange will fail
const auth = betterAuth({
  database: drizzleAdapter(db, { provider: 'pg', schema }),
  secondaryStorage: createRedisSecondaryStorage(),
  plugins: [
    oauthProvider({
      // ...
    }),
  ],
  session: {
    expiresIn: 60 * 60 * 24 * 7,
    // storeSessionInDatabase: true  <-- missing, causes the bug
  },
})

Expected Behavior

Either:

  1. Automatic: OAuth Provider plugin should automatically require/set storeSessionInDatabase: true when secondaryStorage is present
  2. Clear Error: Throw a descriptive error at initialization: "OAuth Provider requires session.storeSessionInDatabase: true when using secondaryStorage"
  3. Documentation: At minimum, document this requirement clearly

Workaround

Add storeSessionInDatabase: true to session config:

session: {
  storeSessionInDatabase: true,
},

Environment

  • better-auth: 1.4.9
  • @better-auth/oauth-provider: 1.4.9
  • Database: PostgreSQL with Drizzle ORM
  • Secondary Storage: Redis
Originally created by @productdevbook on GitHub (Dec 25, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6995 ## Description When using `secondaryStorage` (e.g., Redis) together with the `oauthProvider` plugin, the OAuth token exchange fails with a confusing error: ``` BetterAuthError: Model "session" not found in schema ``` This error is misleading because the `session` table **does exist** in the Drizzle schema - the issue is that Better Auth's internal schema excludes it. ## Root Cause In `@better-auth/core/src/db/get-tables.ts`, lines 155-158: ```typescript //only add session table if it's not stored in secondary storage ...(!options.secondaryStorage || options.session?.storeSessionInDatabase ? sessionTable : {}), ``` When `secondaryStorage` is configured without `storeSessionInDatabase: true`, the `session` table is excluded from the internal schema. However, the OAuth Provider plugin requires the session table to function properly during token exchange. ## Steps to Reproduce 1. Configure Better Auth with `secondaryStorage` (Redis) 2. Add `oauthProvider` plugin 3. Do NOT set `session.storeSessionInDatabase: true` 4. Attempt OAuth flow - token exchange will fail ```typescript const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'pg', schema }), secondaryStorage: createRedisSecondaryStorage(), plugins: [ oauthProvider({ // ... }), ], session: { expiresIn: 60 * 60 * 24 * 7, // storeSessionInDatabase: true <-- missing, causes the bug }, }) ``` ## Expected Behavior Either: 1. **Automatic**: OAuth Provider plugin should automatically require/set `storeSessionInDatabase: true` when `secondaryStorage` is present 2. **Clear Error**: Throw a descriptive error at initialization: *"OAuth Provider requires `session.storeSessionInDatabase: true` when using `secondaryStorage`"* 3. **Documentation**: At minimum, document this requirement clearly ## Workaround Add `storeSessionInDatabase: true` to session config: ```typescript session: { storeSessionInDatabase: true, }, ``` ## Environment - better-auth: 1.4.9 - @better-auth/oauth-provider: 1.4.9 - Database: PostgreSQL with Drizzle ORM - Secondary Storage: Redis
GiteaMirror added the lockedbug labels 2026-04-17 19:21:34 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Dec 25, 2025):

This is a confirmed issue: when you use secondaryStorage (like Redis) without setting session.storeSessionInDatabase: true, Better Auth omits the session table from its internal schema, which the OAuth Provider plugin requires. This leads to the misleading error BetterAuthError: Model "session" not found in schema, even though the table exists in your Drizzle schema. The OAuth Provider plugin references the session model as a foreign key, so the table must be present for token exchange to work correctly see code.

Setting storeSessionInDatabase: true in your session config is the confirmed workaround and is required for this setup example. There is currently no automatic enforcement, warning, or documentation in the codebase for this requirement see code.

Recommended improvements would be: (1) automatically enforce or warn about this config, (2) throw a clear error at initialization, or (3) document the requirement. For now, always set storeSessionInDatabase: true when using the OAuth Provider plugin with secondaryStorage to avoid this error.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:3691199660 --> @dosubot[bot] commented on GitHub (Dec 25, 2025): <!-- Answer --> This is a confirmed issue: when you use secondaryStorage (like Redis) without setting `session.storeSessionInDatabase: true`, Better Auth omits the session table from its internal schema, which the OAuth Provider plugin requires. This leads to the misleading error `BetterAuthError: Model "session" not found in schema`, even though the table exists in your Drizzle schema. The OAuth Provider plugin references the session model as a foreign key, so the table must be present for token exchange to work correctly [see code](https://github.com/better-auth/better-auth/blob/35fcd1aa89b6e9978d7b34ec971066d6a8f1e4e3/packages/oauth-provider/src/schema.ts#L1-L297). Setting `storeSessionInDatabase: true` in your session config is the confirmed workaround and is required for this setup [example](https://github.com/better-auth/better-auth/issues/2007#issuecomment-2897117162). There is currently no automatic enforcement, warning, or documentation in the codebase for this requirement [see code](https://github.com/better-auth/better-auth/blob/35fcd1aa89b6e9978d7b34ec971066d6a8f1e4e3/packages/core/src/db/get-tables.ts#L150-L158). Recommended improvements would be: (1) automatically enforce or warn about this config, (2) throw a clear error at initialization, or (3) document the requirement. For now, always set `storeSessionInDatabase: true` when using the OAuth Provider plugin with secondaryStorage to avoid this error. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/48c7afcf-4580-4758-922a-ebc5c6c68f96?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/48c7afcf-4580-4758-922a-ebc5c6c68f96?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/48c7afcf-4580-4758-922a-ebc5c6c68f96?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/48c7afcf-4580-4758-922a-ebc5c6c68f96?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/48c7afcf-4580-4758-922a-ebc5c6c68f96?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/48c7afcf-4580-4758-922a-ebc5c6c68f96?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/48c7afcf-4580-4758-922a-ebc5c6c68f96?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20is%20a%20confirmed%20issue%3A%20when%20you%20use%20secondaryStorage%20%28like%20Redis%29%20without%20setting%20%60session.storeSessionInDatabase%3A%20true%60%2C%20Better%20Auth%20omits%20the%20session%20table%20from%20its%20internal%20schema%2C%20which%20the%20OAuth%20Provider%20plugin%20requires.%20This%20leads%20to%20the%20misleading%20error%20%60BetterAuthError%3A%20Model%20%22session%22%20not%20found%20in%20schema%60%2C%20even%20though%20the%20table%20exists%20in%20your%20Drizzle%20schema.%20The%20OAuth%20Provider%20plugin%20references%20the%20session%20model%20as%20a%20foreign%20key%2C%20so%20the%20table%20must%20be%20present%20for%20token%20exchange%20to%20work%20correctly%C2%A0%5Bsee%20code%5D%28https%3A//github.com/better-auth/better-auth/blob/35fcd1aa89b6e9978d7b34ec971066d6a8f1e4e3/packages/oauth-provider/src/schema.ts%23L1-L297%29.%0A%0ASetting%20%60storeSessionInDatabase%3A%20true%60%20in%20your%20session%20config%20is%20the%20confirmed%20workaround%20and%20is%20required%20for%20this%20setup%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/2007%23issuecomment-2897117162%29.%20There%20is%20currently%20no%20automatic%20enforcement%2C%20warning%2C%20or%20documentation%20in%20the%20codebase%20for%20this%20requirement%C2%A0%5Bsee%20code%5D%28https%3A//github.com/better-auth/better-auth/blob/35fcd1aa89b6e9978d7b34ec971066d6a8f1e4e3/packages/core/src/db/get-tables.ts%23L150-L158%29.%0A%0ARecommended%20improvements%20would%20be%3A%20%281%29%20automatically%20enforce%20or%20warn%20about%20this%20config%2C%20%282%29%20throw%20a%20clear%20error%20at%20initialization%2C%20or%20%283%29%20document%20the%20requirement.%20For%20now%2C%20always%20set%20%60storeSessionInDatabase%3A%20true%60%20when%20using%20the%20OAuth%20Provider%20plugin%20with%20secondaryStorage%20to%20avoid%20this%20error.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/6995)
Author
Owner

@bytaesu commented on GitHub (Dec 25, 2025):

Hi @productdevbook, thanks for reaching out! I'll look into this

<!-- gh-comment-id:3691538645 --> @bytaesu commented on GitHub (Dec 25, 2025): Hi @productdevbook, thanks for reaching out! I'll look into this
Author
Owner

@bytaesu commented on GitHub (Dec 25, 2025):

This issue has been shared with the team, and we'll resolve it shortly! cc @dvanmali

<!-- gh-comment-id:3691671858 --> @bytaesu commented on GitHub (Dec 25, 2025): This issue has been shared with the team, and we'll resolve it shortly! cc @dvanmali
Author
Owner

@dvanmali commented on GitHub (Dec 25, 2025):

Thanks for the find @productdevbook, added in an error to catch this. Much appreciated 💯

<!-- gh-comment-id:3691821607 --> @dvanmali commented on GitHub (Dec 25, 2025): Thanks for the find @productdevbook, added in an error to catch this. Much appreciated 💯
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#28018