RORK LABJP
CLOUD — Rork Max compiles native Swift on a fleet of cloud Macs, so you never download Xcode or need to own a MacPLATFORM — Rork Max targets iPhone, iPad, Apple Watch, and Vision Pro, and reaches games, widgets, and Live ActivitiesSHIP — Build in the browser, preview through a streaming simulator, install on device via QR code, and submit to the App Store without leaving RorkSPLIT — Regular Rork generates cross-platform apps with React Native and Expo. Reach for it to ship broadly and fast, and for Max when you need Apple-specific depthCREDIT — The free tier works out to roughly five prompts a week. It helps to budget the cost of trying something separately from the cost of finishing itPRICE — Rork Max sits on the $200/month Max plan, while regular Rork starts free with paid plans from $25/monthCLOUD — Rork Max compiles native Swift on a fleet of cloud Macs, so you never download Xcode or need to own a MacPLATFORM — Rork Max targets iPhone, iPad, Apple Watch, and Vision Pro, and reaches games, widgets, and Live ActivitiesSHIP — Build in the browser, preview through a streaming simulator, install on device via QR code, and submit to the App Store without leaving RorkSPLIT — Regular Rork generates cross-platform apps with React Native and Expo. Reach for it to ship broadly and fast, and for Max when you need Apple-specific depthCREDIT — The free tier works out to roughly five prompts a week. It helps to budget the cost of trying something separately from the cost of finishing itPRICE — Rork Max sits on the $200/month Max plan, while regular Rork starts free with paid plans from $25/month
Articles/Dev Tools
Dev Tools/2026-06-19Advanced

Hardening API Calls in Rork Apps: Token Refresh, Retry, and Idempotency

The fetch Rork generates is left fragile against expired tokens, flaky signal, and double sends. Here is a design that consolidates token refresh, retry with backoff, and idempotency keys into a single client layer, with implementation code and operational numbers.

Rork525React Native218API7Authentication8Idempotency

Premium Article

On a freshly shipped Rork app, I got reports that "please log in again" appeared now and then. It was hard to reproduce and did not always show on a specific action. Watching patiently on my own device, I saw it tended to appear on the first action after the app had been idle for a while.

The cause was an expired access token. The fetch Rork generated does attach the token and send, but it does not handle the chore of quietly refreshing and re-sending when the token has expired. The expiry was surfacing on screen as an authentication error.

As an indie developer running apps that involve payments and sync, I have learned that network robustness maps directly to review scores. Here I want to record the design that takes Rork's raw fetch and consolidates token refresh, retry, and idempotency into a single client layer, with the implementation code.

The weaknesses in the generated fetch

The networking code Rork first emits tends to settle into this shape.

async function getProfile() {
  const res = await fetch(`${API}/me`, {
    headers: { Authorization: `Bearer ${token}` },
  });
  return res.json();
}

This code has three weaknesses. First, it does not refresh an expired token; it returns the error as is. Second, it gives up instantly even on a momentary signal drop. Third, a double tap on a submit button or a re-send after timeout can execute the same action twice. Handle these separately in each screen and the code scatters and gaps appear. That is exactly why we consolidate the layer that handles networking into one place.

Consolidating token refresh into one place

The first thing to solve is the expiry. When the server returns 401, get a new access token with the refresh token and replay the original request.

The pitfall here is simultaneous requests. When several calls all receive 401 at the moment the app resumes, each one launches a refresh and the refreshes pile up. To avoid this, share a single Promise while refreshing is in flight.

let refreshing: Promise<string> | null = null;
 
async function refreshToken(): Promise<string> {
  if (!refreshing) {
    refreshing = fetch(`${API}/auth/refresh`, {
      method: "POST",
      body: JSON.stringify({ refreshToken: store.refreshToken }),
    })
      .then((r) => r.json())
      .then((d) => {
        store.accessToken = d.accessToken;
        return d.accessToken as string;
      })
      .finally(() => {
        refreshing = null;
      });
  }
  return refreshing;        // Concurrent callers await the same refresh
}

By sharing refreshing, no matter how many requests receive 401 at once, the actual refresh stays at one. In my setup this single move nearly stopped the "please log in again" reports.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Consolidate token refresh in one place and collapse simultaneous requests into a single refresh
How to tell which errors are worth retrying, and concrete exponential backoff with a cap
An idempotency-key design that prevents duplicate charges and double posts from re-sends
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Rork Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

Dev Tools2026-07-24
Collapsing Duplicate Requests Into One: A Reference-Counted Single-Flight Layer
When several components fire the same API call at launch, you get a burst of identical requests. Here is a single-flight layer that shares one in-flight promise instead, avoiding the trap of handing out a failed promise forever and the trap of one caller's abort cancelling everyone, with the real network numbers alongside.
Dev Tools2026-07-14
Adding a Single Zod Validation Boundary to Rork's Generated Fetch Code
The network code Rork generates implicitly trusts the shape of the response. When the API shifts, the screen quietly goes blank. Here is how to slip a single Zod parse layer between the generated UI and the network to make failures predictable, with numbers from real operation.
Dev Tools2026-08-01
Every Experiment Kept Landing on the Same Devices: Hash Choice and Salt Position, Measured Across a Million IDs
On-device experiment assignment looked fine in isolation, then collapsed the moment two experiments ran side by side. Here is what one million IDs revealed about four hash functions, why the culprit was the concatenation order rather than hash quality, and the implementation I settled on, with the measured numbers.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →