RORK LABJP
MAX — Rork Max generates native Swift apps for iPhone, iPad, Apple Watch, Apple TV, Vision Pro, and iMessagePUBLISH — Rork Max ships 2-click App Store publishing and runs $200/monthRN — The standard Rork builds native iOS/Android apps with React Native (Expo) — the quicker path to a working appPRICE — Rork is free to start, with paid plans from $25/monthFUND — Rork raised $2.8M from a16z; the platform now sees 743k+ monthly visits with 85% growthFLOW — Describe your app in plain English and Rork generates deployable code that can use the camera, notifications, and moreMAX — Rork Max generates native Swift apps for iPhone, iPad, Apple Watch, Apple TV, Vision Pro, and iMessagePUBLISH — Rork Max ships 2-click App Store publishing and runs $200/monthRN — The standard Rork builds native iOS/Android apps with React Native (Expo) — the quicker path to a working appPRICE — Rork is free to start, with paid plans from $25/monthFUND — Rork raised $2.8M from a16z; the platform now sees 743k+ monthly visits with 85% growthFLOW — Describe your app in plain English and Rork generates deployable code that can use the camera, notifications, and more
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.

Rork425React Native168API5Authentication6Idempotency

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-06-19
Rebuilding Rork's Generated Form Screens for Real Use: react-hook-form and zod
Rork's generated forms look fine on screen but fall apart on a real device: the whole screen re-renders on every keystroke, the keyboard hides the submit button, and slow networks invite double submits. Here is how I rebuild them with react-hook-form and zod, from an indie developer's point of view.
Dev Tools2026-06-19
When Rork-Built Lists Stutter: Designing Image Caching and Prefetch
A FlatList from Rork starts stuttering once the images pile up. Here is how I restore smoothness with expo-image caching, recyclingKey, prefetch, and a move to FlashList, with the device numbers I measured.
Dev Tools2026-06-19
Adding a Minimal Test Safety Net to Rork-Generated Screens
You add one new screen to a Rork app, and a completely unrelated paywall check quietly breaks. This is how to bolt a minimal automated test safety net onto generated code with Jest and React Native Testing Library — protecting only the three places that hurt when they break.
📚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 →