[PR #8211] [MERGED] fix: persist refreshed idToken in getAccessToken #16068

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8211
Author: @GautamBytes
Created: 2/28/2026
Status: Merged
Merged: 2/28/2026
Merged by: @himself65

Base: canaryHead: fix/persist-id-token


📝 Commits (1)

  • 99dc28b fix: persist idToken on access token refresh

📊 Changes

2 files changed (+310 additions, -0 deletions)

View changed files

📝 packages/better-auth/src/api/routes/account.test.ts (+309 -0)
📝 packages/better-auth/src/api/routes/account.ts (+1 -0)

📄 Description

Closes #8191

Summary

Fixes a bug where getAccessToken auto-refresh returned a new idToken but did not persist it, causing stale identity claims to remain in storage.

Root Cause

In packages/better-auth/src/api/routes/account.ts, the getAccessToken auto-refresh path updated:

  • accessToken
  • accessTokenExpiresAt
  • refreshToken
  • refreshTokenExpiresAt

but omitted idToken from updatedData.

Fix

Updated getAccessToken auto-refresh persistence to include:

  • idToken: newTokens?.idToken || account.idToken

This mirrors the existing fallback behavior already used in the refreshToken endpoint.

Files Changed

  • packages/better-auth/src/api/routes/account.ts
  • packages/better-auth/src/api/routes/account.test.ts

Tests Added

  1. Database persistence regression test
    Verifies refreshed idToken is persisted to the account DB record during getAccessToken auto-refresh.

  2. Stateless cookie persistence regression test
    Verifies refreshed idToken is persisted in encrypted better-auth.account_data cookie (JWE) when running without DB adapter.

Both tests use MSW to mock sequential OAuth token responses (old id_token on initial exchange, new id_token on refresh).


Summary by cubic

Persist refreshed idToken during getAccessToken auto-refresh so identity claims stay up to date in DB and stateless cookie modes. Prevents stale idToken from being stored after a token refresh.

  • Bug Fixes
    • Persist idToken when refreshing access tokens in getAccessToken (with fallback to existing value; mirrors refreshToken endpoint).
    • Added regression tests for DB persistence and stateless account cookie (JWE).

Written for commit 99dc28b9e4. 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/8211 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 2/28/2026 **Status:** ✅ Merged **Merged:** 2/28/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/persist-id-token` --- ### 📝 Commits (1) - [`99dc28b`](https://github.com/better-auth/better-auth/commit/99dc28b9e4b51e196468ded25a2dc5b8b98fca85) fix: persist idToken on access token refresh ### 📊 Changes **2 files changed** (+310 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/account.test.ts` (+309 -0) 📝 `packages/better-auth/src/api/routes/account.ts` (+1 -0) </details> ### 📄 Description ## Related Issue Closes #8191 ## Summary Fixes a bug where `getAccessToken` auto-refresh returned a new `idToken` but did not persist it, causing stale identity claims to remain in storage. ## Root Cause In `packages/better-auth/src/api/routes/account.ts`, the `getAccessToken` auto-refresh path updated: - `accessToken` - `accessTokenExpiresAt` - `refreshToken` - `refreshTokenExpiresAt` but omitted `idToken` from `updatedData`. ## Fix Updated `getAccessToken` auto-refresh persistence to include: - `idToken: newTokens?.idToken || account.idToken` This mirrors the existing fallback behavior already used in the `refreshToken` endpoint. ## Files Changed - `packages/better-auth/src/api/routes/account.ts` - `packages/better-auth/src/api/routes/account.test.ts` ## Tests Added 1. **Database persistence regression test** Verifies refreshed `idToken` is persisted to the account DB record during `getAccessToken` auto-refresh. 2. **Stateless cookie persistence regression test** Verifies refreshed `idToken` is persisted in encrypted `better-auth.account_data` cookie (JWE) when running without DB adapter. Both tests use MSW to mock sequential OAuth token responses (`old id_token` on initial exchange, `new id_token` on refresh). <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Persist refreshed idToken during getAccessToken auto-refresh so identity claims stay up to date in DB and stateless cookie modes. Prevents stale idToken from being stored after a token refresh. - **Bug Fixes** - Persist idToken when refreshing access tokens in getAccessToken (with fallback to existing value; mirrors refreshToken endpoint). - Added regression tests for DB persistence and stateless account cookie (JWE). <sup>Written for commit 99dc28b9e4b51e196468ded25a2dc5b8b98fca85. 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:22:23 -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#16068