Clerk sign-in keeps redirecting in a loop

On this page

Short answer

Something sends the visitor back to sign-in from the page they just reached. Check three things: a middleware rule that also covers the sign-in page, a force-redirect URL that points at a protected page, and mismatched Clerk keys. Simplify the middleware, point redirects at a page that doesn't bounce, and check that the publishable and secret keys come from the same Clerk instance.

Find a fix pack

Checklists and prompts for Supabase security, Stripe payments and deploys. Each pack is $24, or $49 for all three.

The exact error you'll see

Clerk's backend counts its own redirects. After three handshake round-trips, it stops, treats the visitor as signed out, and writes one of these lines to the server log (your terminal locally, or the function logs on your host):

Clerk: Refreshing the session token resulted in an infinite redirect loop. This usually means that your Clerk instance keys do not match - make sure to copy the correct publishable and secret keys from the Clerk dashboard.

Source: https://github.com/clerk/javascript/blob/main/packages/backend/src/tokens/request.ts (opens in a new tab)

For satellite domains:

Clerk: Satellite-domain authentication resulted in an infinite redirect loop. Check that this request is using a configured primary or satellite domain for the production instance.

Source: https://github.com/clerk/javascript/blob/main/packages/backend/src/tokens/request.ts (opens in a new tab)

Applies to

  • @clerk/nextjs with clerkMiddleware() in proxy.ts, or middleware.ts on Next.js 15 and earlier
  • Astro apps using Clerk on Netlify
  • Clerk satellite domains (satelliteAutoSync)
  • The *_SIGN_IN_URL, *_FORCE_REDIRECT_URL and *_FALLBACK_REDIRECT_URL environment variables

Why it happens

  • The middleware protects the sign-in page itself. Clerk now says "Middleware is not the best place to protect routes," and createRouteMatcher() "is deprecated and logs a runtime deprecation warning when called."
  • A force redirect points back at the gate. A force redirect URL is used "regardless of what page the user was on before," and overrides redirect_url.
  • The keys don't match. The source message above names this as the usual cause of a handshake loop.
  • Astro on Netlify. Clerk says Netlify's caching causes "infinite redirects that cause an app to fail," including on previews that use development keys.
  • satelliteAutoSync: true. It redirects a satellite app "to the primary domain on every first visit." The default is false.

How to fix it, step by step

  1. Reduce the middleware to Clerk's default, export default clerkMiddleware(), with Clerk's documented matcher. Move the protection into the page, layout or route handler that reads the data.
  2. Add { debug: true } as the second argument to clerkMiddleware(), reload once, and read the debug logs in your terminal.
  3. In the Clerk dashboard, copy the publishable key and secret key again from the same instance, set them on your host, and redeploy.
  4. Set NEXT_PUBLIC_CLERK_SIGN_IN_URL to your real sign-in path. Set the fallback URLs (NEXT_PUBLIC_CLERK_SIGN_IN_FALLBACK_REDIRECT_URL and NEXT_PUBLIC_CLERK_SIGN_UP_FALLBACK_REDIRECT_URL) to a page that doesn't redirect to sign-in. Clerk recommends defining both the sign-in and sign-up variables.
  5. Remove any *_FORCE_REDIRECT_URL unless you need it. If you keep it, make sure its target doesn't send signed-in users back to sign-in.
  6. For Astro on Netlify, use production API keys with a new domain, as Clerk recommends.
  7. Leave satelliteAutoSync unset unless you want a redirect on every first visit.

What the docs don't spell out

  • The loop can end with you signed out. Clerk's backend stops after its redirect counter reaches 3 and returns a signed-out state. If the browser stops bouncing but you land signed out, look for the log line above. (request.ts (opens in a new tab), handshake.ts (opens in a new tab))
  • Check force URLs before fallback URLs. Fallbacks are only used when there is no redirect_url. A force URL always wins, so a force URL that points at a protected page sends every sign-in there. (Redirect URLs (opens in a new tab))

How to check it worked

  • In a private window, open a protected page, sign in once, and land on the page you set. The address bar stops changing.
  • Opening /sign-in directly shows Clerk's form instead of redirecting.
  • The server logs show no "infinite redirect loop" line.

Sources

  1. https://clerk.com/docs/reference/nextjs/clerk-middleware (opens in a new tab): default matcher, proxy.ts vs middleware.ts, debug: true, createRouteMatcher deprecation, satelliteAutoSync
  2. https://clerk.com/docs/guides/development/customize-redirect-urls (opens in a new tab): env variable names, force vs fallback behavior, define both
  3. https://clerk.com/docs/guides/development/deployment/astro (opens in a new tab): Netlify caching causes infinite redirects
  4. https://github.com/clerk/javascript/blob/main/packages/backend/src/tokens/request.ts (opens in a new tab): exact loop messages
  5. https://github.com/clerk/javascript/blob/main/packages/backend/src/tokens/handshake.ts (opens in a new tab): redirect counter limit of 3

Tool to consider: Clerk.

Find a fix pack

Checklists and prompts for Supabase security, Stripe payments and deploys. Each pack is $24, or $49 for all three.