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 flag | Value / default | Applies to | Source |
|---|---|---|---|
jwt | First-time login: save the access token, its expiry and the refresh token | Current Auth.js guide | guide |
expires_at | Epoch seconds: multiply by 1000 for Date.now() comparison; after refresh use Math.floor(Date.now() / 1000 + newTokens.expires_in) | Current Auth.js guide | guide |
refresh_token | Save the new token when returned; otherwise retain token.refresh_token | Provider-dependent rotation | guide |
session | Copy token.error into session.error and return the session | JWT session callback | guide |
error | Set "RefreshTokenError" on refresh failure; the marker is the guide's application convention | Current Auth.js guide | guide |
signIn("google") | Reauthenticate on session?.error === "RefreshTokenError"; substitute the actual provider | Current Next.js example | error 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) | Cause | Fix | Source |
|---|---|---|---|
Missing refresh_token | token.refresh_token is missing | Obtain the provider's required consent/access mode through sign-in; do not attempt refresh without it | guide |
RefreshTokenError | Token refresh failed, or competing callers reused an invalidated single-use token | Coordinate refresh; for a rejected canonical credential expose the marker and call provider sign-in | guide, 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