Coalesce concurrent server-side refreshes of the same refresh token across clients

Open
#315 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript

Research direction

Start by reading createServerClient in the ssr package and how it forwards global.fetch and the auth options to auth-js; then look at how refreshingDeferred and auth.lock dedupe refreshes inside a single client. The proposal offers two designs (a refreshCoalescing.reuseMs option vs. a default process-wide, token-keyed lock), and picking one is the first decision to write down before coding. Done means concurrent refreshes keyed on the refresh token share one Auth call and its response, with a test covering parallel requests reusing the same token.

Written by the indexing model from the issue text.

Description

Refresh tokens are single-use, and on the server every request builds its own createServerClient. When a browser sends several requests at once with an expired access token (a navigation plus prefetches, parallel route handlers, a few islands), each client refreshes the same refresh token. The first request rotates it. The others either get refresh_token_already_used or race inside the reuse interval, and the responses write different sessions into the same cookies.

auth-js dedupes refreshes inside one client (refreshingDeferred), but nothing is shared between server clients, so this can't be solved by the app without wrapping fetch.

What we do today

We pass createServerClient a global.fetch that coalesces POST /token?grant_type=refresh_token by refresh token, process-wide (a registry on globalThis, so separate module graphs in Next.js share it). The first call goes to Auth, concurrent callers wait for it, and the response is reused for 10 seconds so a request that arrives just after the rotation still gets the new session instead of a reuse error.

Proposal

An opt-in server-side refresh coalescing option, for example:

createServerClient(url, key, {
  cookies,
  auth: { refreshCoalescing: { reuseMs: 10_000 } },
})

or a pluggable lock (auth.lock exists for browsers) that createServerClient sets to a process-wide, token-keyed implementation by default. Either way, the key must be the refresh token (or a hash of it), never anything a client controls, and the shared response must only be replayed to callers that presented the same token.

Dominant language
TypeScript
Stars
216
Forks
35
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from supabase/ssr

All issues in supabase/ssr

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.