[PR #7901] [CLOSED] fix(oauth-provider): defensive Date coercion for Drizzle/SQLite compatibility #15868

Closed
opened 2026-04-13 10:16:33 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7901
Author: @jayy-77
Created: 2/10/2026
Status: Closed

Base: canaryHead: fix/oauth-provider-date-coercion-7819


📝 Commits (1)

  • 2fde5a6 fix(oauth-provider): defensive Date coercion for Drizzle/SQLite compatibility

📊 Changes

4 files changed (+146 additions, -132 deletions)

View changed files

📝 packages/oauth-provider/src/introspect.ts (+14 -13)
📝 packages/oauth-provider/src/register.ts (+6 -6)
📝 packages/oauth-provider/src/token.ts (+113 -112)
📝 packages/oauth-provider/src/utils/index.ts (+13 -1)

📄 Description

Problem

The oauth-provider plugin calls .getTime() directly on date values retrieved from the database (expiresAt, createdAt). When using the Drizzle adapter with SQLite, these values may be returned as ISO strings or Unix timestamps rather than native Date objects, causing:

TypeError: expiresAt.getTime is not a function

This affects token introspection, client registration responses, and id token generation.

Changes

  • packages/oauth-provider/src/utils/index.ts: Added a toDate() utility that defensively coerces string | number | Date to a Date object. Only calls new Date() if the value is not already a Date instance.
  • packages/oauth-provider/src/introspect.ts: Wrapped 4 .getTime() calls with toDate() — opaque access token expiresAt/createdAt and refresh token expiresAt/createdAt.
  • packages/oauth-provider/src/register.ts: Wrapped 2 .getTime() calls with toDate() — client expiresAt and createdAt in schemaToOAuth.
  • packages/oauth-provider/src/token.ts: Wrapped 1 .getTime() call with toDate()session.createdAt used for id token auth_time claim.

Test plan

  • npx tsc --noEmit in packages/oauth-provider — clean
  • npx tsc --noEmit in packages/better-auth — clean

Fixes #7819


Summary by cubic

Defensively coerce DB date fields to Date objects in oauth-provider to avoid runtime errors with Drizzle/SQLite. This fixes bad exp/iat calculations in introspection, client registration responses, and id token auth_time. Fixes #7819.

  • Bug Fixes
    • Added toDate(value) utility to normalize string|number|Date to Date.
    • Wrapped getTime() usages in introspect (access/refresh token exp/iat) and register (client expiresAt/createdAt).
    • Wrapped session.createdAt in token.ts when computing id token auth_time.

Written for commit 2fde5a6678. 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/7901 **Author:** [@jayy-77](https://github.com/jayy-77) **Created:** 2/10/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/oauth-provider-date-coercion-7819` --- ### 📝 Commits (1) - [`2fde5a6`](https://github.com/better-auth/better-auth/commit/2fde5a6678809bb1d873ea5e72d518450af95998) fix(oauth-provider): defensive Date coercion for Drizzle/SQLite compatibility ### 📊 Changes **4 files changed** (+146 additions, -132 deletions) <details> <summary>View changed files</summary> 📝 `packages/oauth-provider/src/introspect.ts` (+14 -13) 📝 `packages/oauth-provider/src/register.ts` (+6 -6) 📝 `packages/oauth-provider/src/token.ts` (+113 -112) 📝 `packages/oauth-provider/src/utils/index.ts` (+13 -1) </details> ### 📄 Description ## Problem The oauth-provider plugin calls `.getTime()` directly on date values retrieved from the database (`expiresAt`, `createdAt`). When using the Drizzle adapter with SQLite, these values may be returned as ISO strings or Unix timestamps rather than native `Date` objects, causing: ``` TypeError: expiresAt.getTime is not a function ``` This affects token introspection, client registration responses, and id token generation. ## Changes - **`packages/oauth-provider/src/utils/index.ts`**: Added a `toDate()` utility that defensively coerces `string | number | Date` to a `Date` object. Only calls `new Date()` if the value is not already a `Date` instance. - **`packages/oauth-provider/src/introspect.ts`**: Wrapped 4 `.getTime()` calls with `toDate()` — opaque access token `expiresAt`/`createdAt` and refresh token `expiresAt`/`createdAt`. - **`packages/oauth-provider/src/register.ts`**: Wrapped 2 `.getTime()` calls with `toDate()` — client `expiresAt` and `createdAt` in `schemaToOAuth`. - **`packages/oauth-provider/src/token.ts`**: Wrapped 1 `.getTime()` call with `toDate()` — `session.createdAt` used for id token `auth_time` claim. ## Test plan - `npx tsc --noEmit` in `packages/oauth-provider` — clean - `npx tsc --noEmit` in `packages/better-auth` — clean Fixes #7819 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Defensively coerce DB date fields to Date objects in oauth-provider to avoid runtime errors with Drizzle/SQLite. This fixes bad exp/iat calculations in introspection, client registration responses, and id token auth_time. Fixes #7819. - **Bug Fixes** - Added toDate(value) utility to normalize string|number|Date to Date. - Wrapped getTime() usages in introspect (access/refresh token exp/iat) and register (client expiresAt/createdAt). - Wrapped session.createdAt in token.ts when computing id token auth_time. <sup>Written for commit 2fde5a6678809bb1d873ea5e72d518450af95998. 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-04-13 10:16:33 -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#15868