clerk / javascript

Official Javascript repository for Clerk authentication
https://clerk.com
MIT License
1.12k stars 246 forks source link

Infinite redirect loop in NextJS development environment #1436

Closed ghost closed 1 year ago

ghost commented 1 year ago

Description

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

screenshot

Package + Version

{  
  "dependencies": {
    "@clerk/nextjs": "^4.21.12",
    "next": "13.4.7",
  }
}
dimkl commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

GuillaumeDesforges commented 1 year ago

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 commented 1 year ago

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

nkeil commented 1 year ago

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

ValkovMirec commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

aviroopjana commented 1 year ago

Yeah it also worked for me

satishkumarsajjan commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Tried syncing my time, still the same issue

royanger commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

I"m using Windows 11. I changed my time and time zone. I cleared the cache and cookies and I'm still getting the same error. Then I tried another web browser and still the same... Thanks for your time

El dom, 20 de ago de 2023 1:49 p. m., Timi @.***> escribió:

I am using @.***/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?

— Reply to this email directly, view it on GitHub https://github.com/clerkinc/javascript/issues/1436#issuecomment-1685352120, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUXR5LLQAMTKQI4F4D5HM43XWJE2DANCNFSM6AAAAAAZYYYU6Q . You are receiving this because you commented.Message ID: @.***>

emLuc86dev commented 1 year ago

[image: image.png]

El dom, 20 ago 2023 a la(s) 19:07, Emilio Salvia @.***) escribió:

I"m using Windows 11. I changed my time and time zone. I cleared the cache and cookies and I'm still getting the same error. Then I tried another web browser and still the same... Thanks for your time

El dom, 20 de ago de 2023 1:49 p. m., Timi @.***> escribió:

I am using @.***/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?

— Reply to this email directly, view it on GitHub https://github.com/clerkinc/javascript/issues/1436#issuecomment-1685352120, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUXR5LLQAMTKQI4F4D5HM43XWJE2DANCNFSM6AAAAAAZYYYU6Q . You are receiving this because you commented.Message ID: @.***>

jokereven commented 1 year ago

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

thanks when i setup then restart computer that okay;

emLuc86dev commented 1 year ago

Hi mate I'm having this bug yet... I've tried everything, cleared cookies and cache, changed time and time zone automatically. I deleted the project and created it from scratch and still have the same infinity loop bug....

El lun, 21 ago 2023 a la(s) 09:21, zhou jing @.***) escribió:

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: image] https://user-images.githubusercontent.com/68476321/260267986-2a4edf9f-dee4-436b-9a1b-a48cb9913469.png

thanks when i setup then restart computer that okay;

— Reply to this email directly, view it on GitHub https://github.com/clerkinc/javascript/issues/1436#issuecomment-1686319785, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUXR5LM5OT3J6BQGIENK4QTXWNOFDANCNFSM6AAAAAAZYYYU6Q . You are receiving this because you were mentioned.Message ID: @.***>

fanckush commented 1 year ago

@emLuc86dev maybe this https://github.com/clerkinc/javascript/issues/1566 is the cause of your bug? it also causes infinite loop but for a different reason that the timezone one

creative-tutorials commented 1 year ago

I"m using Windows 11. I changed my time and time zone. I cleared the cache and cookies and I'm still getting the same error. Then I tried another web browser and still the same... Thanks for your time El dom, 20 de ago de 2023 1:49 p. m., Timi @.> escribió: I am using @./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? — Reply to this email directly, view it on GitHub <#1436 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUXR5LLQAMTKQI4F4D5HM43XWJE2DANCNFSM6AAAAAAZYYYU6Q . You are receiving this because you commented.Message ID: @.***>

I want you to sign out of your app first, so sign out, clear cookies, local storage, session storage, and browsing data for your app. Then change the time and zone, as I screenshot earlier, then try again. One thing I've noticed is to try different devices. You can either host it online to test it or use ngrok. If still that didn't work send you can DM me the project let me take a look

creative-tutorials commented 1 year ago

@emLuc86dev maybe this #1566 is the cause of your bug? it also causes infinite loop but for a different reason that the timezone one

Now this one I've not seen before, @emLuc86dev try using the debug mode and share what error you seem to be having

creative-tutorials commented 1 year ago

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

thanks when i setup then restart computer that okay;

You're welcome @jokereven

royanger commented 1 year ago

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.

Is your staging using Dev keys from Clerk? If so, we would recommend either:

royanger commented 1 year ago

I"m using Windows 11. I changed my time and time zone. I cleared the cache and cookies and I'm still getting the same error. Then I tried another web browser and still the same...

Did you add debug: true (see https://github.com/clerkinc/javascript/issues/1436#issuecomment-1668728895)? If so, what were the results of the log?

abdulramon-jemil commented 1 year ago

This error happens after a long time of not using the app. The session token will expire at that time, and a page on the site needs to load to refresh. This is what I've noticed; not sure if it's what's happening.

For me, if I don't use the app for a long time, and then I visit a protected page hoping that I'll be redirected, typically, the error shows up, but at that time, Clerk would have done refresh, and when I reload the page, everything works fine again. I am sure that it's not an issue with my time.

Has anyone else noticed the same thing?

abdulramon-jemil commented 1 year ago

I'm now pretty sure that this is what is happening, at least for me.

The token is usually expired when loading the app after a long time.

abdulramon-jemil commented 1 year ago

Maybe someone else should try it

jescalan commented 1 year ago

This issue has been open for nearly 2 months and now encapsulates several different issues from different people, some resolved, some maybe not. In an effort to clean things up a bit, going to close this out. Please do not interpret this as us not caring - we know that this error has been difficult for many, and have been discussing how to improve it internally. Please feel free to open a new issue, ideally with a reproduction, for anyone still encountering this issue!

salehelsayed commented 1 year ago

I am still encountering this issue. syncing the timezone didn't solve the problem

creative-tutorials commented 1 year ago

I am still encountering this issue. syncing the timezone didn't solve the problem

Which Operating System are you encountering this issue on?

salehelsayed commented 1 year ago

I am still encountering this issue. syncing the timezone didn't solve the problem

Which Operating System are you encountering this issue on?

Connecting to the application from Windows 11 (tried from 2 different laptops). application is running on an Ubuntu EC2 instance.

creative-tutorials commented 1 year ago

I am still encountering this issue. syncing the timezone didn't solve the problem

Which Operating System are you encountering this issue on?

Connecting to the application from Windows 11 (tried from 2 different laptops). application is running on an Ubuntu EC2 instance.

Okay, did you try syncing your time?

salehelsayed commented 1 year ago

I am still encountering this issue. syncing the timezone didn't solve the problem

Which Operating System are you encountering this issue on?

Connecting to the application from Windows 11 (tried from 2 different laptops). application is running on an Ubuntu EC2 instance.

Okay, did you try syncing your time?

Yes, I followed the steps in the thread. didn't fix the problem

creative-tutorials commented 1 year ago

I am still encountering this issue. syncing the timezone didn't solve the problem

Which Operating System are you encountering this issue on?

Connecting to the application from Windows 11 (tried from 2 different laptops). application is running on an Ubuntu EC2 instance.

Okay, did you try syncing your time?

Yes, I followed the steps in the thread. didn't fix the problem

Okay, I would need you to turn these off and on again, and then click sync. After that clear your cookies and hit Ctrl + Shift + R

image

jameslporter commented 12 months ago

I'm running this app behind varnish and realized with debug logging turned on that the nextUrl variable is localhost:3000 instead of the hostname the browser is using. The behavior I'm experience is everything works absolutely fine until the session is expired.

I looked around the clerk dashboard and found that in paths everything was setup to use clerk components, whereas I have the app hosting it. I adjusted those settings but the redirect loop remains. I do see the clerkUrl is set to what I configured in paths now. I've previously cleared all browser data and was able to sign-in. Hit this again and not feeling confident in shipping what we have to customers.

docent-backend-dashboard-1  | [clerk debug start: authMiddleware]
docent-backend-dashboard-1  |   URL debug, {
docent-backend-dashboard-1  |     "url": "http://localhost:3000/",
docent-backend-dashboard-1  |     "method": "GET",
docent-backend-dashboard-1  |     "headers": "{\"accept\":\"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\",\"accept-encoding\":\"gzip, deflate\",\"accept-language\":\"en-US,en;q=0.9\",\"cache-control\":\"\",\"connection\":\"keep-alive\",\"cookie\":\"__client_uat=1696990712; __session=eyJhbGciOiJSUzI1NiIsImtpZCI6Imluc18yVnRiOWM1RU9vek51U0d1T25taG1nZVBCSFMiLCJ0eXAiOiJKV1QifQ.eyJhenAiOiJodHRwOi8vZGFzaGJvYXJkLmxvY2FsLmRvY2VudGFwcC5jbyIsImV4cCI6MTY5NzIzMTY1NywiaWF0IjoxNjk3MjMxNTk3LCJpc3MiOiJodHRwczovL2dsb3Jpb3VzLWJ1Y2stMzYuY2xlcmsuYWNjb3VudHMuZGV2IiwibmJmIjoxNjk3MjMxNTg3LCJvcmdfaWQiOiJvcmdfMlZ0Y0ZnNllkOTB4UGlURk9TSDlKbjhFMWluIiwib3JnX3JvbGUiOiJhZG1pbiIsIm9yZ19zbHVnIjoiZG9jZW50Iiwic2lkIjoic2Vzc18yV2JBNERHZVhlanBUOG10enlzMjZLMWczWmsiLCJzdWIiOiJ1c2VyXzJWdGNCeFRLamVTdU1HdjZWT0ZLTVlKREtnWiJ9.Q9V8M5wLp6wyLur-Mkt5MNE8PDy-iYXzlxFQXcqudDJp4b70srbvgo6Ug_9UQDcQ-uZqLh0S_WvoJqA3uKc4Md2D4e6BXaBxgr6igvRhKw8J9ier4G4yEsA36wr0TMRowSugj23YRFDhaKeIOZM112guQJj_uPlD1KmziVbw_51iRHL59mLuD5JBL-9JzSEEci87BflPUqW4O4s_58G6rLo1X2umnhAA1yQ24tlAAXHvpYyw_iI5cKQUoISrbJADGEZMl1CMRl7I8RpdnjwsWgsr_IVXdrwnk_7ANGYvQxzJ25u9eASR4MKyrcWWofH6mi1Duqfnyw13xp2kSUYgew; __clerk_db_jwt=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJkZXYiOiJkdmJfMldHczdhWDhOSFRpVWUzUDMyUUN2YnB2dnJ6IiwiaWQiOiJjbGllbnRfMldHc0RSN1dRblUzaHJrRk1ZdHFGRTVOaHdBIiwicm90YXRpbmdfdG9rZW4iOiI3Z28yOTd1aGNjOGtzc29wd211eHhtNDZ2eWs4OWIzY3RvMmIwajdoIn0.cWnvJyZMKcPu7XCZcH2vMd-Yl-8-XTneHE3ZFwwOLj-ndc37famsBGxhxGQ6HgAK7KZAt6CXwV8f8kMYJJB2N7OjeVfylZ0ZiMFibyP3fJ0qaLHc1rB-NrlQIvJd8C3ECVQM5iF84kF6OTRShqOh9jI0hUv5xup_qGPJ_wfBnZMNp3C18hixZiJljaidmKInEiG-8DAAXu-9irIa6xPSQFjBbCm1MumKFMRCrqVeCA5J2MynQHBdAyryZYNYnlEc4TpAGuDXyTw1wDZpe9j5H2cMsY7AWylQqrbCsW0O4Wjq11O_BNCArTzBobc3uKCyIT8cHXaUoLjh8fWtnl2O2g\",\"host\":\"dashboard.local.docentapp.co\",\"referer\":\"http://dashboard.local.docentapp.co/\",\"sec-fetch-mode\":\"cors\",\"upgrade-insecure-requests\":\"1\",\"user-agent\":\"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\",\"via\":\"1.1 6af71816a8f5 (Varnish/7.3)\",\"x-forwarded-for\":\"172.19.0.1\",\"x-invoke-output\":\"\",\"x-invoke-path\":\"\",\"x-invoke-query\":\"\",\"x-middleware-invoke\":\"1\",\"x-varnish\":\"33111\"}",
docent-backend-dashboard-1  |     "nextUrl": "http://localhost:3000/",
docent-backend-dashboard-1  |     "clerkUrl": "http://dashboard.local.docentapp.co/"
docent-backend-dashboard-1  |   }
docent-backend-dashboard-1  |   Options debug, {
docent-backend-dashboard-1  |     "debug": true,
docent-backend-dashboard-1  |     "beforeAuth": false,
docent-backend-dashboard-1  |     "afterAuth": false
docent-backend-dashboard-1  |   }
docent-backend-dashboard-1  |   authenticateRequest state is interstitial, {
docent-backend-dashboard-1  |     "status": "interstitial",
docent-backend-dashboard-1  |     "reason": "token-expired",
docent-backend-dashboard-1  |     "message": "JWT is expired. Expiry date: Fri, 13 Oct 2023 21:14:17 GMT, Current date: Fri, 20 Oct 2023 16:22:34 GMT. (reason=token-expired, token-carrier=cookie)",
docent-backend-dashboard-1  |     "frontendApi": "glorious-buck-36.clerk.accounts.dev",
docent-backend-dashboard-1  |     "publishableKey": "<redacted>",
docent-backend-dashboard-1  |     "isSatellite": false,
docent-backend-dashboard-1  |     "domain": "",
docent-backend-dashboard-1  |     "proxyUrl": "",
docent-backend-dashboard-1  |     "signInUrl": "/user",
docent-backend-dashboard-1  |     "isSignedIn": false,
docent-backend-dashboard-1  |     "isInterstitial": true,
docent-backend-dashboard-1  |     "isUnknown": false
docent-backend-dashboard-1  |   }
docent-backend-dashboard-1  | [clerk debug end: authMiddleware] (@clerk/nextjs=4.25.6,next=13.4.19)
JsIqbal commented 11 months ago

The issues you encountered are likely due to dependency deprecation errors, which are not uncommon in JavaScript. Here's a step-by-step solution to address these problems:

  1. Begin by updating the clerkjs and nextjs versions in your package.json file to the latest available versions.

  2. Delete the .next build file to ensure a fresh build with the updated dependencies.

  3. Clear the npm cache by running the following command:

npm cache clean --force

After clearing the cache, perform a fresh install by executing:

npm install

These steps should resolve the issues you encountered. If you encountered these problems while dockerizing your client-server architecture, it's essential to ensure that the dependencies are updated and compatible. If you have any further questions or need assistance, feel free to reach out to me via direct message. Good luck with resolving the issue!

rk1 commented 11 months ago

I was getting this error when using node v20, switching back to 18 resolved it for me

cungbuituananh commented 11 months ago

I was getting this error when using node v20, switching back to 18 resolved it for me

This is not working with me :(

creative-tutorials commented 11 months ago

I'm shocked this issue has been ongoing to this extent. I'm running node v20 and this works fine. But I get this error once in a while, It's not an everytime thing now. I suggest updating your timezone, and dependencies. Make sure you're running the latest version of @clerk/nextjs

If you are using pnpm simply just run the following commands

  1. Check for outdated dependencies
$ pnpm outdated
  1. Update the dependencies
pnpm up --latest

[!IMPORTANT]
If you have updated the @clerk/nextjs dependency and still you face this issue. You could then try downgrading your Node.js version to v18.