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.
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.
Applies to
- @clerk/nextjs with
clerkMiddleware()inproxy.ts, ormiddleware.tson Next.js 15 and earlier - Astro apps using Clerk on Netlify
- Clerk satellite domains (
satelliteAutoSync) - The
*_SIGN_IN_URL,*_FORCE_REDIRECT_URLand*_FALLBACK_REDIRECT_URLenvironment 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 isfalse.
How to fix it, step by step
- Reduce the middleware to Clerk's default,
export default clerkMiddleware(), with Clerk's documentedmatcher. Move the protection into the page, layout or route handler that reads the data. - Add
{ debug: true }as the second argument toclerkMiddleware(), reload once, and read the debug logs in your terminal. - In the Clerk dashboard, copy the publishable key and secret key again from the same instance, set them on your host, and redeploy.
- Set
NEXT_PUBLIC_CLERK_SIGN_IN_URLto your real sign-in path. Set the fallback URLs (NEXT_PUBLIC_CLERK_SIGN_IN_FALLBACK_REDIRECT_URLandNEXT_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. - Remove any
*_FORCE_REDIRECT_URLunless you need it. If you keep it, make sure its target doesn't send signed-in users back to sign-in. - For Astro on Netlify, use production API keys with a new domain, as Clerk recommends.
- Leave
satelliteAutoSyncunset 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-indirectly shows Clerk's form instead of redirecting. - The server logs show no "infinite redirect loop" line.
Sources
- https:/
/ : default matcher, proxy.ts vs middleware.ts,clerk. com/ docs/ reference/ nextjs/ clerk- middleware (opens in a new tab) debug: true, createRouteMatcher deprecation,satelliteAutoSync - https:/
/ : env variable names, force vs fallback behavior, define bothclerk. com/ docs/ guides/ development/ customize- redirect- urls (opens in a new tab) - https:/
/ : Netlify caching causes infinite redirectsclerk. com/ docs/ guides/ development/ deployment/ astro (opens in a new tab) - https:/
/ : exact loop messagesgithub. com/ clerk/ javascript/ blob/ main/ packages/ backend/ src/ tokens/ request. ts (opens in a new tab) - https:/
/ : redirect counter limit of 3github. com/ clerk/ javascript/ blob/ main/ packages/ backend/ src/ tokens/ handshake. ts (opens in a new tab)
Related fixes
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.