fix: skip token-refresh redirect in database mode#480
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the server-side authenticated API client to avoid the preemptive /api/auth/token-refresh redirect when Better Auth is configured in database-backed mode, since token refresh state is persisted in the DB rather than via rotated cookies.
Changes:
- Skip the “token near expiry” redirect flow when
isDatabaseModeis enabled. - Add unit tests for
getAuthenticatedClientcovering both database mode (no redirect) and cookie mode (redirect on near-expiry).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/lib/api-client.ts |
Gates the near-expiry redirect logic behind !isDatabaseMode to avoid unnecessary Route Handler redirects in DB mode. |
src/lib/api-client.test.ts |
Introduces Vitest coverage validating redirect behavior differences between database vs cookie session modes. |
rdimitrov
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
When
DATABASE_URLis set (database mode), users hit an infinite redirect loop immediately after login:This results in a "Too Many Redirects" browser error and makes the app completely unusable.
Root cause
In database mode, Better Auth is configured with
storeAccountCookie: false(auth.ts:88), meaning theaccount_datacookie is never written. However,isTokenNearExpiry()always tries to read that cookie to check token expiry:This creates an infinite loop:
/cataloggetAuthenticatedClient()→isTokenNearExpiry()→ noaccount_datacookie (DB mode) → returnstrue/api/auth/token-refresh?redirect=%2Fcatalog/catalogThe bug affects any OIDC provider (Okta, Azure AD, mock) whenever
DATABASE_URLis configured — including the defaultdocker-compose.yamlwhich hardcodesDATABASE_URL.Fix
Guard the preemptive refresh redirect behind
!isDatabaseMode. In database mode, Better Auth reads and writes tokens directly to the DB — no Route Handler cookie workaround is needed.The cookie-based redirect to
/api/auth/token-refreshwas introduced specifically to handle the limitation that Server Components cannot write cookies. In database mode this limitation does not apply, so Better Auth handles token refresh transparently viagetAccessToken().Test plan
make compose-down && make compose-upusing Okta + docker-composeDATABASE_URL) with mock OIDC (pnpm dev)