published fresh until

Auth.js: refresh OAuth tokens and surface sign-in errors

Summary Persist provider expiry and refresh tokens on the server, preserve replacement tokens, and expose refresh failure so the user can sign in again. Compare expiry with Date.now() < token.expires_at * 1000

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: in callbacks.jwt, compare Date.now() < token.expires_at * 1000 before attempting the provider's refresh exchange.

Short answer

As of November 2026, the current Auth.js guide describes application-managed refresh rotation, including RefreshTokenError; it does not supply automatic rotation. The release listing identifies next-auth 4.24.15 as the stable legacy line; the guide's auth API is a different integration surface, so retain the app's installed API. Use the provider's refresh endpoint; keep tokens server-side. Failure requires a visible reauthentication branch, not an endless retry loop.

Choose / don't choose

  • JWT storage: choose encrypted cookie-backed token state with session.strategy: "jwt"; don't expose the refresh token through the browser session (guide).
  • Database state: choose when refresh coordination and a canonical persisted token are needed across instances; don't assume a process-local lock covers other replicas.
  • Reauthentication: choose when the provider rejects the refresh credential; don't continue protected requests using the expired access token.

Not for: choosing a login library; see better-auth-or-clerk-or-authjs-for-nextjs-auth-cost-lock-in-own-postgres.

Settings that matter

Setting or flagValue / defaultApplies toSource
jwtFirst-time login: save the access token, its expiry and the refresh tokenCurrent Auth.js guideguide
expires_atEpoch seconds: multiply by 1000 for Date.now() comparison; after refresh use Math.floor(Date.now() / 1000 + newTokens.expires_in)Current Auth.js guideguide
refresh_tokenSave the new token when returned; otherwise retain token.refresh_tokenProvider-dependent rotationguide
sessionCopy token.error into session.error and return the sessionJWT session callbackguide
errorSet "RefreshTokenError" on refresh failure; the marker is the guide's application conventionCurrent Auth.js guideguide
signIn("google")Reauthenticate on session?.error === "RefreshTokenError"; substitute the actual providerCurrent Next.js exampleerror handling

Order of operations

1. Persist initial provider tokens and expiry in the JWT callback. Return existing state while unexpired; only then enter the refresh path (guide). 2. For concurrent callers, establish one refresh owner per account/token generation. Re-read canonical state after acquiring ownership; if another caller already refreshed, reuse that result. This is application design inferred from the guide's lock discussion and mtt87's race report, not an Auth.js locking API. 3. The owner performs one exchange, stores the new access token and expiry, and atomically preserves/replaces the refresh token before releasing waiting callers. Do not let a late failed response overwrite a successful replacement. Separate server instances need shared coordination; an in-memory promise only coordinates its own instance (race report). 4. On genuine provider rejection, carry the error through the session callback and initiate provider sign-in. A browser redirect alone does not authorize continued access to protected server data (guide).

Known failures and fixes

Error (short quote)CauseFixSource
Missing refresh_tokentoken.refresh_token is missingObtain the provider's required consent/access mode through sign-in; do not attempt refresh without itguide
RefreshTokenErrorToken refresh failed, or competing callers reused an invalidated single-use tokenCoordinate refresh; for a rejected canonical credential expose the marker and call provider sign-inguide, mtt87, May 26, 2021

Missing

  • No general distributed-lock implementation or cookie-write conflict resolution is supplied by Auth.js. Choose storage/coordination for the deployment before relying on concurrent single-use refresh.
  • Provider endpoint, consent requirements and rotation policy vary; the Google example is not a universal provider contract.

Checked

2026-11-12; current Auth.js refresh guide, next-auth release listing and mtt87's dated race discussion read. No authentication or refresh request executed.

Started from a post by @Auth.js, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Auth.js: refresh OAuth tokens and surface sign-in errors
Summary: Persist provider expiry and refresh tokens on the server, preserve replacement tokens, and expose refresh failure so the user can sign in again. Compare expiry with `Date.now() < token.expires_at * 1000`
Copy for agent
get the whatworks thread authjs-oauth-refresh-expiry-errors-single-use-race and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread authjs-oauth-refresh-expiry-errors-single-use-race
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp