Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite redirect loop in NextJS development environment #1436

Closed
1 of 16 tasks
ghost opened this issue Jun 29, 2023 · 97 comments
Closed
1 of 16 tasks

Infinite redirect loop in NextJS development environment #1436

ghost opened this issue Jun 29, 2023 · 97 comments

Comments

@ghost
Copy link

ghost commented Jun 29, 2023

Description

I'm getting this error constantly after logging into an account.
This is development environment and running on localhost

screenshot

Package + Version

  • @clerk/clerk-js
  • @clerk/clerk-react
  • @clerk/nextjs
  • @clerk/remix
  • @clerk/types
  • @clerk/themes
  • @clerk/localizations
  • @clerk/clerk-expo
  • @clerk/backend
  • @clerk/clerk-sdk-node
  • @clerk/shared
  • @clerk/fastify
  • @clerk/chrome-extension
  • gatsby-plugin-clerk
  • build/tooling/chore
  • other:
{  
  "dependencies": {
    "@clerk/nextjs": "^4.21.12",
    "next": "13.4.7",
  }
}
@dimkl
Copy link
Contributor

dimkl commented Jun 29, 2023

Hello @Aayushpatil77 and thank you for opening this issue.
If the issue is not resolved after trying the 3 Reasons described in the error message, could you provide a repository that reproduces this error to investigate it more and get back to you?

@adarsh-gupta101
Copy link

image
Same issue here.

Using Next.js and getting an infinite redirect. But when we refresh the page again, it will work as expected.

GitHub repo example->https://github.com/adarsh-gupta101/Next-full-stack-editor

@ghost
Copy link
Author

ghost commented Jun 30, 2023

@GuillaumeDesforges
Copy link

Had sort of the same issue. It works fine on localhost, but when I try to proxy (e.g. using ngrok) the browser gets stuck in an infinite loop. In my case, React does not display any error message.

I tried to run the debugger in Chrome; found this line to be the "culprit":
https://github.com/clerkinc/javascript/blob/019992f2063ba8580446edf98c760eabcc8fc71a/packages/backend/src/tokens/interstitial.ts#L101-L102

Why would my page suddenly have an empty fragment? I would expect that it is added in the call itself e.g.

-                        window.location.href = window.location.href;
+                        window.location.href = window.location.href + "#";

@GuillaumeDesforges
Copy link

GuillaumeDesforges commented Jul 3, 2023

This was with version @clerk/nextjs==4.19.0, seems fixed in @clerk/nextjs==4.21.13

@dimkl dimkl changed the title Infinite redirect loop Infinite redirect loop in NextJS development environment Jul 7, 2023
@nkeil
Copy link

nkeil commented Jul 10, 2023

I got this error after switching auth providers to Clerk, I got it to work by clearing my old cookies.

@ValkovMirec
Copy link

ValkovMirec commented Jul 14, 2023

I got the same problem. Fresh install of T3, installed clerk, followed quick start docs, first npm run dev and i got this error. Tried everything. Skewing time updating, checked keys. It doesn't work

@aviroopjana
Copy link

This was with version @clerk/nextjs==4.19.0, seems fixed in @clerk/nextjs==4.21.13

I am using "@clerk/nextjs": "^4.23.2" this version and still getting the error... If anyone knows how to fix it please do let me know

@catchshyam
Copy link

On my windows PC, syncing with the time server solved the issue. It might occur again, but I am good for now

@aviroopjana
Copy link

Yeah it also worked for me

@satishkumarsajjan
Copy link

I am facing the same issue as of now. It was working fine the day before i was developing the app. when i ran the Next app again the next day, it's showing the infinite loop error. Is their any particular reason??

@satishkumarsajjan
Copy link

Clerk needs your system clock to be in sync with your current timezone. I synced my system timezone correctly. The clerk is working fine.

@aviroopjana
Copy link

Clerk needs your system clock to be in sync with your current timezone. I synced my system timezone correctly. The clerk is working fine.

Yeah that was the problem

@OkolieOKS234
Copy link

Tried syncing my time, still the same issue

@royanger
Copy link
Member

royanger commented Aug 8, 2023

If you are experiencing this in NextJS, please add debug: true to your authMiddleware. See https://clerk.com/docs/nextjs/middleware#middleware-argument-types. This will give you debugging loads which can produce more information.

If you see an error like the one below, then it is indicating your clock is out of sync.

[0] "message": "JWT issued at date claim (iat) is in the future. Issued at date: Mon Jul 10 2023 19:40:49 GMT+0200 (Central European Summer Time); Current date: Mon Jul 10 2023 19:40:38 GMT+0200 (Central European Summer Time); (reason=token-not-active-yet, token-carrier=cookie)"

@creative-tutorials
Copy link

creative-tutorials commented Aug 12, 2023

When I try clearing the cookies it works for me, but after saving a few changes to my code, it doesn't work anymore

image

GitHub Repo --> https://github.com/creative-tutorials/resubase

@creative-tutorials
Copy link

If you are experiencing this in NextJS, please add debug: true to your authMiddleware. See https://clerk.com/docs/nextjs/middleware#middleware-argument-types. This will give you debugging loads which can produce more information.

If you see an error like the one below, then it is indicating your clock is out of sync.

[0] "message": "JWT issued at date claim (iat) is in the future. Issued at date: Mon Jul 10 2023 19:40:49 GMT+0200 (Central European Summer Time); Current date: Mon Jul 10 2023 19:40:38 GMT+0200 (Central European Summer Time); (reason=token-not-active-yet, token-carrier=cookie)"

Thanks for the tip, it shows much clearer information now, the state for isSignedIn seems to be treated as false, with a JWT message

@royanger
Copy link
Member

Issued at date: Mon Jul 10 2023 19:40:49 GMT+0200 (Central European Summer Time); Current date: Mon Jul 10 2023 19:40:38 GMT+0200 (Central European Summer Time); (reason=token-not-active-yet, token-carrier=cookie)

This is confirming that your clock is out of sync and in the future. Syncing your clock with a time server should resolve this error.

@creative-tutorials
Copy link

Issued at date: Mon Jul 10 2023 19:40:49 GMT+0200 (Central European Summer Time); Current date: Mon Jul 10 2023 19:40:38 GMT+0200 (Central European Summer Time); (reason=token-not-active-yet, token-carrier=cookie)

This is confirming that your clock is out of sync and in the future. Syncing your clock with a time server should resolve this error.

Thank you very much, it turns out that toggling "Set time automatically" and "Set time zone automatically", with synchronizing the time, and properly setting my time zone did the trick.

image

@IAmTahazzot
Copy link

IAmTahazzot commented Aug 19, 2023

On my windows PC, syncing with the time server solved the issue. It might occur again, but I am good for now

Does anybody encounter it on production app? where end users doesn't sync with time server from windows settings?
I mean, We developer fixing it on our device but what if it happens same with end users ? Where they didn't sync with time server?

(Mine also fixed with time server sync)

@creative-tutorials
Copy link

creative-tutorials commented Aug 19, 2023

On my windows PC, syncing with the time server solved the issue. It might occur again, but I am good for now

Does anybody encouter it on production app? where use doesn't sync with time server from windows settings? I mean, We developer fixing it on our device but what if it happens same with end users ? Where they didn't sync with time server?

(Mine also fixed with time server sync)

Yikes, you're definitely right about that, there has to be like a workaround around this.

@emLuc86dev
Copy link

I am using "@clerk/nextjs": "^4.23.2". I have changed the time and time zone and I still getting the same error ... Does anyone have a solutions?

@kachar
Copy link

kachar commented Aug 20, 2023

We've noticed. that certain users have bad cookie values which causes the infinite loop.

The cookie name is __client_uat and the bad value is 0 instead of some timestamp like 1692272951

Once we clear this cookie the redirect loops are gone.

Our observation of this cookie is that it gets a bad value when we switch between the subdomains of our app - Production / Staging.

@creative-tutorials
Copy link

We've noticed. that certain users have bad cookie values which causes the infinite loop.

The cookie name is __client_uat and the bad value is 0 instead of some timestamp like 1692272951

Once we clear this cookie the redirect loops are gone.

Our observation of this cookie is that it gets a bad value when we switch between the subdomains of our app - Production / Staging.

I didn't get this bad value, All I needed was to change the time zone. Thanks for sharing by the way

@creative-tutorials
Copy link

creative-tutorials commented Aug 20, 2023

I am using "@clerk/nextjs": "^4.23.2". I have changed the time and time zone and I still getting the same error ... Does anyone have a solutions?

Which OS are you using If I may ask @emLuc86dev

@emLuc86dev
Copy link

emLuc86dev commented Aug 20, 2023 via email

@Ontorok
Copy link

Ontorok commented Dec 26, 2023

After changing node 20.10.0 to node 18.12.1, it working fine.

@creative-tutorials
Copy link

If anyone is still having this issue, please make sure your date-time and timezone are set to automatically update. clear cookies, and run your application in incognito mode. If this doesn't work try reconfiguring your application with Clerk again by following the docs.

@amateurbeekeeper
Copy link

amateurbeekeeper commented Jan 7, 2024

cleared cookies, changed node version (v18.18.2), and she works :--) thanks all

@jameslporter
Copy link

jameslporter commented Jan 7, 2024 via email

@stx-chris
Copy link

stx-chris commented Jan 15, 2024

We are experiencing the exact same error since releasing our production environment. The issue might be tied to the __client_uat cookie which is available twice if both the develop and the main instance are running on the same domain.

We have the following scenario:

  • development on a.b.c.mysite.io
  • main on app.mysite.io

This is the cookie we see on main:

  • __client_uat = <timestamp>

This is what we see on develop:

  • First cookie, domain "a.b.c.mysite.io": __client_uat = <timestamp>
  • Second cookie, domain ".mysite.io": __client_uat = 0

Once one has visited the main site with their browser and switches to develop to test something, the site is trapped in an infinite loop with the error referring to the said cookie, ie.

The Cookie '__client_uat' has been rejected for invalid domain.

We have to painfull clear the cookies, refresh everything and refrain from accessing the live site in the same session. It never occurs on localhost. Anybody else having this issue?

(Node v18.17.0, @clerk/nextjs v4.29.3, in Docker)

@carlelgstroem
Copy link

carlelgstroem commented Jan 22, 2024

I just ran into the same issue after updating my node to v.21. I resolved the issue by downgrading node to v.18 and updating nextjs to v.14.1.0. Hope this might help someone!

@creative-tutorials
Copy link

I just ran into the same issue after updating my node to v.21. I resolved the issue by downgrading node to v.18 and updating nextjs to v.14.1.0. Hope this might help someone!

This will help a lot. Thanks!

But I want to know who else is still having this issue. I'm on node v.20.10.0 and I haven't noticed these issues for a long time, it comes back yes, but when I sync my time, I don't notice it again. The issue only comes back whenever I run into a low battery on my laptop.

@eraoul
Copy link

eraoul commented Jan 29, 2024

I'm running into this on node v21.6.1

@creative-tutorials
Copy link

I'm running into this on node v21.6.1

Try syncing your clock please

@flean
Copy link

flean commented Jan 31, 2024

I just ran into the same issue after updating my node to v.21. I resolved the issue by downgrading node to v.18 and updating nextjs to v.14.1.0. Hope this might help someone!

This will help a lot. Thanks!

But I want to know who else is still having this issue. I'm on node v.20.10.0 and I haven't noticed these issues for a long time, it comes back yes, but when I sync my time, I don't notice it again. The issue only comes back whenever I run into a low battery on my laptop.

We are also having this issue. Still have no clue how to fix it.

@jameslporter
Copy link

jameslporter commented Jan 31, 2024 via email

@eraoul
Copy link

eraoul commented Jan 31, 2024

@jameslporter yes I just figured out it must be something like that. I was running prod on mydomain.com and a staging instance on a subdomain, staging.mydomain.com. This caused the infinite loop. I found out that if I cleared my cookies etc. in Chrome and then viewed the subdomain, it would work (at least for a while), but once I viewed the prod instance things would be screwed up again.

Great to hear there is future work planned to support this across subdomains; would rather not have to pay for extra domains just to support a staging environment (unless that's a best practice for some reason etc.)

@dnyn92
Copy link

dnyn92 commented Feb 14, 2024

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

@alamenai
Copy link

alamenai commented Mar 9, 2024

@dimkl Github repo :- https://github.com/Aayushpatil77/ecommerce-dashboard

Not found!

@alamenai
Copy link

alamenai commented Mar 9, 2024

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

It happened only in development or even the production?

@dnyn92
Copy link

dnyn92 commented Mar 14, 2024

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

It happened only in development or even the production?

It only helped for the initial page load but same issue after I refreshed. I have figured out the final fix now. Seem to be a Nextjs Issue. Upgrading to 14.1.3 solved the issue eventually. vercel/next.js#55648

@creative-tutorials
Copy link

creative-tutorials commented Mar 22, 2024

Mainly

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

It happened only in development or even the production?

It only helped for the initial page load but same issue after I refreshed. I have figured out the final fix now. Seem to be a Nextjs Issue. Upgrading to 14.1.3 solved the issue eventually. vercel/next.js#55648

Mainly everyone should just upgrade to Next.js 14.1.3. It worked for me as I upgraded as well.

@yuvrajzingh
Copy link

Mainly

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

It happened only in development or even the production?

It only helped for the initial page load but same issue after I refreshed. I have figured out the final fix now. Seem to be a Nextjs Issue. Upgrading to 14.1.3 solved the issue eventually. vercel/next.js#55648

Mainly everyone should just upgrade to Next.js 14.1.3. It worked for me as I upgraded as well.

Yes, upgrading my Next.js ('^14.2.1') worked for me. Just do npm install next@latest and it should install the latest Next.js which should resolve this issue with clerk.

@nikos-pblworks
Copy link

We are experiencing the exact same error since releasing our production environment. The issue might be tied to the __client_uat cookie which is available twice if both the develop and the main instance are running on the same domain.

We have the following scenario:

  • development on a.b.c.mysite.io
  • main on app.mysite.io

This is the cookie we see on main:

  • __client_uat = <timestamp>

This is what we see on develop:

  • First cookie, domain "a.b.c.mysite.io": __client_uat = <timestamp>
  • Second cookie, domain ".mysite.io": __client_uat = 0

Once one has visited the main site with their browser and switches to develop to test something, the site is trapped in an infinite loop with the error referring to the said cookie, ie.

The Cookie '__client_uat' has been rejected for invalid domain.

We have to painfull clear the cookies, refresh everything and refrain from accessing the live site in the same session. It never occurs on localhost. Anybody else having this issue?

(Node v18.17.0, @clerk/nextjs v4.29.3, in Docker)

Same issue here. @stx-chris did you figure out a solution? Was it solved by updating to the latest packages?

@alamenai
Copy link

Hi guys, I figured out that the issue is related to the local storage and session's keys. When I delete them it works as expected. I guess this happens when the authentication failed. This is my only for my case.

@hieutrluu
Copy link

I got into redirect loop with these packages
clearning localstorage and session keys does not help

"next": "^14.2.3",
"@clerk/nextjs": "4.30.1",

@jescalan
Copy link
Contributor

jescalan commented May 7, 2024

@hieutrluu Could you please open a new issue or reach out to [email protected] with a minimal reproduction? Thank you!

@Vipigal
Copy link

Vipigal commented May 13, 2024

I found a rather dumb reason why I was being infinitly redirected in my end. In the middleware.ts setup, I had my RouteMatcher configured like so:

import { NextResponse } from "next/server";

const isProtectedRoute = createRouteMatcher(["/(.*)"]);

export default clerkMiddleware((auth, req) => {
  if (isProtectedRoute(req)) {
    auth().protect();
  }

  return NextResponse.next();
});

export const config = {
  matcher: ["/((?!.+.[w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
}; 

The problem was that the createRouteMatcher method was configured to redirect every path starting with "/", which Im assuming included the /sign-up page and created the loop.

Changing the line to const isProtectedRoute = createRouteMatcher(["/"]); fixed my problem.

(I added the wildcard cause I wanted to make every route private, seems I might have to find another way...)

@creative-tutorials
Copy link

Everyone has different ways on how to solve this issue, mainly I'd just upgrade all my packages to latest, and make sure my date-and-time are correct

@i-am-chitti
Copy link

I faced the redirect issue due to middleware. Make sure, you include the sign in/sign up path in public path routes. Next, redirect won't happen anymore.

const isPublicRoute = createRouteMatcher(['/agency/sign-in(.*)', '/agency/sign-up(.*)', '/_next(.*)'])

@DevTaehong
Copy link

Clerk needs your system clock to be in sync with your current timezone. I synced my system timezone correctly. The clerk is working fine.

On my Windows machine, I turned off set time zone automatically and set time automatically and it worked...!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests