Skip to main content
Cloud & Security · Developer Error Resolution Lab

Fix Supabase SSR Error: 'AuthApiError: Invalid Refresh Token: Refresh Token Not Found' in Next.js

Fix Supabase `@supabase/ssr` cookie synchronization race conditions in Next.js App Router proxy/middleware and Server Components.

Triggered Error Message:

AuthApiError: Invalid Refresh Token: Refresh Token Not Found (or Invalid Refresh Token: Already Used)

Customize Solution for Your Environment
# 1. Inspect your Supabase JWT expiration & iat claims locally (offline):
npx castov jwt <YOUR_SB_ACCESS_TOKEN>

# 2. Ensure @supabase/ssr >= 0.5.2 is installed and updateSession() runs in src/proxy.ts (Next.js 16) or src/middleware.ts.

Root Cause & Mechanism Analysis

Why does this error occur?

Server Components in Next.js cannot write HTTP response cookies directly. When an expired Supabase access token is refreshed inside a Server Component without a `proxy.ts` / `middleware.ts` refreshing and persisting the rotated token first, the single-use refresh token is consumed and discarded.

Frequently Asked Questions

Why should I use `supabase.auth.getUser()` instead of `getSession()` on the server?
`getSession()` reads the unverified cookie payload directly, whereas `getUser()` revalidates the token with Supabase Auth and safely triggers token rotation in middleware.
DEVSECOPS & CI/CD PARTNER2 Slots Open

Need Zero-Knowledge .env Secret Sync Across Your Engineering Team?

Explore Castov Env Vault for client-side AES-256-GCM encrypted environment sharing, or sponsor this engineering hub.

Browse All 54 Guides