#3953·puter

puter.js: 401 `token_auth_failed` never replays the request, so every call fails with Unauthorized after a successful re-login

Author: rasadregmiCreated Sep 26, 2026Updated Sep 26, 2026

Summary

In a browser (puter.env === 'web'), a 401 carrying the legacy code token_auth_failed opens the sign-in flow but never re-issues the original request. Once the user signs in successfully, the call that triggered the prompt still rejects with { status: 401, message: 'Unauthorized' }, so the user has to repeat the action by hand.

reauth_required — the other reauth signal — recovers transparently, so the two signals behave differently for the same situation.

Steps to reproduce

  1. Open the Puter web app in a browser.

  2. Make the stored bearer token fail authentication (e.g. sign the session out server-side, or sign in as another account on the same device so the in-tab token is no longer valid).

  3. Call any authenticated SDK API from the devtools console, e.g.

    javascript
    await puter.fs.stat('/');
  4. The sign-in prompt opens. Complete it.

Actual: the promise rejects with Unauthorized (status 401) even though the new token is valid. The request is sent once.

Expected: the request is replayed once with the fresh token and resolves — the documented behaviour of fetchUrl:

A 401 carrying a reauth signal — the reauth flow is driven first and, on success, the request is replayed once with the fresh token (transparent recovery).

This affects both SDK transport paths, so it hits driver calls too (puter.fs.*, puter.kv.*, puter.drivers.call(...)).

Root cause

resolveReauth() in src/puter-js/src/lib/networkUtils.js returns one of replay / reject / null, and both callers act only on action === 'replay':

  • src/puter-js/src/lib/utils.js:105 (handle_resp, generic XHR path)
  • src/puter-js/src/lib/networkUtils.js:608 (classifyRetry, driver-call path)

The reauth_required branch returns { action: 'replay' } after puter.triggerReauth() resolves (line 98). The token_auth_failed branch awaits puter.ui.authenticateWithPuter() and then falls out of the if to return null (line 129) — so the caller is told "this 401 is not reauth-recoverable" and the successful re-login is thrown away.

The backend still emits this code, always from the auth gate before the route handler runs:

  • src/backend/core/http/middleware/gates.ts:51
  • src/backend/controllers/fs/LegacyFSController.ts:1252 and :1260
  • src/backend/controllers/auth/AuthController.ts:4496

Because the request never reached the handler, replaying it is free of side effects, and both replay paths are already one-shot (ctx.done.add('reauth') in classifyRetry, spec._replayed in replayXhr), so a fix cannot loop.

Suggested fix

Return { action: 'replay' } once authenticateWithPuter() resolves, mirroring the reauth_required branch:

javascript
if (resp?.code === 'token_auth_failed' && puter.env === 'web') {
    if (!interactive) return resolveBackgroundReauth(resp, sentToken);
    try {
        puter.resetAuthToken();
        await puter.ui.authenticateWithPuter();
        return { action: 'replay' };
    } catch (e) {
        // ...unchanged auth_canceled rejection
    }
}

Acceptance criteria

  • A browser call that receives 401 { code: 'token_auth_failed' }, followed by a successful sign-in, is replayed once with the fresh token and resolves on the retry.
  • The replay happens at most once — a token_auth_failed that persists after the replay still surfaces as Unauthorized.
  • The non-interactive path (interactiveReauth: false, background/boot requests) is unchanged: the stale token is dropped, no UI is raised.
  • Dismissing the sign-in prompt still rejects with auth_canceled; no other error shape changes.
  • Regression coverage added to src/puter-js/src/lib/networkUtils.test.js.