javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Infinite redirect loop in NextJS development environment

Open ghost opened this issue 3 years ago • 101 comments

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
  • [x] @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",
  }
}

ghost avatar Jun 29 '23 16:06 ghost

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?

dimkl avatar Jun 29 '23 18:06 dimkl

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

adarsh-gupta101 avatar Jun 30 '23 16:06 adarsh-gupta101

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

ghost avatar Jun 30 '23 16:06 ghost

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 avatar Jul 03 '23 23:07 GuillaumeDesforges

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

GuillaumeDesforges avatar Jul 03 '23 23:07 GuillaumeDesforges

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

nkeil avatar Jul 10 '23 19:07 nkeil

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

ValkovMirec avatar Jul 14 '23 13:07 ValkovMirec

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

aviroopjana avatar Aug 05 '23 21:08 aviroopjana

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

catchshyam avatar Aug 06 '23 13:08 catchshyam

Yeah it also worked for me

aviroopjana avatar Aug 06 '23 17:08 aviroopjana

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 avatar Aug 07 '23 12:08 satishkumarsajjan

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

satishkumarsajjan avatar Aug 07 '23 12:08 satishkumarsajjan

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

aviroopjana avatar Aug 07 '23 12:08 aviroopjana

Tried syncing my time, still the same issue

OkolieOKS234 avatar Aug 07 '23 14:08 OkolieOKS234

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)"

royanger avatar Aug 08 '23 00:08 royanger

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 avatar Aug 12 '23 14:08 creative-tutorials

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

creative-tutorials avatar Aug 12 '23 14:08 creative-tutorials

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.

royanger avatar Aug 13 '23 00:08 royanger

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

creative-tutorials avatar Aug 13 '23 00:08 creative-tutorials

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)

IAmTahazzot avatar Aug 19 '23 06:08 IAmTahazzot