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

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

Which Operating System are you encountering this issue on?

creative-tutorials avatar Oct 15 '23 19:10 creative-tutorials

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.

salehelsayed avatar Oct 16 '23 12:10 salehelsayed

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?

creative-tutorials avatar Oct 17 '23 09:10 creative-tutorials

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

salehelsayed avatar Oct 17 '23 09:10 salehelsayed

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

creative-tutorials avatar Oct 17 '23 09:10 creative-tutorials

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)

jameslporter avatar Oct 20 '23 16:10 jameslporter

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!

geomachine avatar Oct 25 '23 18:10 geomachine

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

rk1 avatar Nov 01 '23 10:11 rk1

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

This is not working with me :(

cungbuituananh avatar Nov 01 '23 11:11 cungbuituananh

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.

creative-tutorials avatar Nov 01 '23 18:11 creative-tutorials

I'm still experiencing this issue with v18 downgrade and npm up --latest. Due to being sick I haven't been signed into prod in over a week and it's handling it fine. Cannot get my local to stop infinite redirect until I delete all my browser data. Just doing it for the site wasn't sufficient in my prior attempts at working around it. Fresh sign-in works fine until 7 days later you get infinitely redirected.

The middleware logging I added looks the same as before: 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: Mon, 06 Nov 2023 15:59:28 GMT. (reason=token-expired, token-carrier=cookie)", docent-backend-dashboard-1 | "frontendApi": ".clerk.accounts.dev", docent-backend-dashboard-1 | "publishableKey": "", 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 | }

jameslporter avatar Nov 06 '23 17:11 jameslporter

it got solved when i installed the latest version of clerk and nextjs

On Mon, 6 Nov 2023 at 23:43, Jim Porter @.***> wrote:

I'm still experiencing this issue with v18 downgrade and npm up --latest. Due to being sick I haven't been signed into prod in over a week and it's handling it fine. Cannot get my local to stop infinite redirect until I delete all my browser data. Just doing it for the site wasn't sufficient in my prior attempts at working around it. Fresh sign-in works fine until 7 days later you get infinitely redirected.

The middleware logging I added looks the same as before: 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: Mon, 06 Nov 2023 15:59:28 GMT. (reason=token-expired, token-carrier=cookie)", docent-backend-dashboard-1 | "frontendApi": ".clerk.accounts.dev", docent-backend-dashboard-1 | "publishableKey": "", 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 | }

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

geomachine avatar Nov 12 '23 11:11 geomachine

I'm still experiencing this issue with v18 downgrade and npm up --latest. Due to being sick I haven't been signed into prod in over a week and it's handling it fine. Cannot get my local to stop infinite redirect until I delete all my browser data. Just doing it for the site wasn't sufficient in my prior attempts at working around it. Fresh sign-in works fine until 7 days later you get infinitely redirected.

The middleware logging I added looks the same as before: 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: Mon, 06 Nov 2023 15:59:28 GMT. (reason=token-expired, token-carrier=cookie)", docent-backend-dashboard-1 | "frontendApi": ".clerk.accounts.dev", docent-backend-dashboard-1 | "publishableKey": "", 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 | }

May I ask which clerk dependency you installed?

creative-tutorials avatar Nov 13 '23 07:11 creative-tutorials

@creative-tutorials turns out it was not a dependency issue at all. I finally figured out on my own that if you are using a hostname other than localhost over http you hit this redirect due https upgrade header. When I opened up the port on my container and accessed the app via localhost:3000 it worked as designed. I mentioned all of this in a support thread on discord. Honestly kind of dissappointed with the lack of support here from Clerk. I had to debug and figure this out, not one staff member even ones I reached out to directly helped me figure out what's going on. If there was FOSS I'd be totally okay with this, but a paid product...not a good look. A callout in the docs is all I've asked but it's fallen on deaf ears. Catching it via software and producing a warning instead of an infinite redirect would be better.

jameslporter avatar Nov 13 '23 12:11 jameslporter

@creative-tutorials turns out it was not a dependency issue at all. I finally figured out on my own that if you are using a hostname other than localhost over http you hit this redirect due https upgrade header. When I opened up the port on my container and accessed the app via localhost:3000 it worked as designed. I mentioned all of this in a support thread on discord. Honestly kind of dissappointed with the lack of support here from Clerk. I had to debug and figure this out, not one staff member even ones I reached out to directly helped me figure out what's going on. If there was FOSS I'd be totally okay with this, but a paid product...not a good look. A callout in the docs is all I've asked but it's fallen on deaf ears. Catching it via software and producing a warning instead of an infinite redirect would be better.

Glad you resolved the issue, didn't even know such a little change could cause such a big issue.

creative-tutorials avatar Nov 13 '23 13:11 creative-tutorials

@creative-tutorials turns out it was not a dependency issue at all. I finally figured out on my own that if you are using a hostname other than localhost over http you hit this redirect due https upgrade header. When I opened up the port on my container and accessed the app via localhost:3000 it worked as designed. I mentioned all of this in a support thread on discord. Honestly kind of dissappointed with the lack of support here from Clerk. I had to debug and figure this out, not one staff member even ones I reached out to directly helped me figure out what's going on. If there was FOSS I'd be totally okay with this, but a paid product...not a good look. A callout in the docs is all I've asked but it's fallen on deaf ears. Catching it via software and producing a warning instead of an infinite redirect would be better.

Hi @jameslporter we are facing the exact same issue where our deployed container shows https://localhost:3000 in the Clerk debug info instead of the actual hostname, which fails with https. Did you find a workaround to make it work with an external hostname?

  URL debug, {
    "url": "https://localhost:3000/",
    "method": "GET",
    "headers": "{...}",
    "nextUrl": "https://localhost:3000/",
    "clerkUrl": "https://localhost:3000/"
  }

stx-chris avatar Nov 15 '23 08:11 stx-chris

I'd suspect that is due to reverse proxy. The only way I can get clerk dev environment working correctly is http://localhost:3000 and that port being open directly to the nodejs container, not the proxy. I use varnish and it's fine for our stage/prod envs.

For clarification I'm guessing based on a week of pulling my hair out. Been asking clerk for clarification in the documentation but it seems it's fallen on dead ears. I've had better support from unpaid open source projects than this.

jameslporter avatar Nov 15 '23 10:11 jameslporter

@creative-tutorials turns out it was not a dependency issue at all. I finally figured out on my own that if you are using a hostname other than localhost over http you hit this redirect due https upgrade header. When I opened up the port on my container and accessed the app via localhost:3000 it worked as designed. I mentioned all of this in a support thread on discord. Honestly kind of dissappointed with the lack of support here from Clerk. I had to debug and figure this out, not one staff member even ones I reached out to directly helped me figure out what's going on. If there was FOSS I'd be totally okay with this, but a paid product...not a good look. A callout in the docs is all I've asked but it's fallen on deaf ears. Catching it via software and producing a warning instead of an infinite redirect would be better.

Hi @jameslporter we are facing the exact same issue where our deployed container shows https://localhost:3000 in the Clerk debug info instead of the actual hostname, which fails with https. Did you find a workaround to make it work with an external hostname?

  URL debug, {
    "url": "https://localhost:3000/",
    "method": "GET",
    "headers": "{...}",
    "nextUrl": "https://localhost:3000/",
    "clerkUrl": "https://localhost:3000/"
  }

I never changed much in my code, but I did change some things in my clerk dashboard tho

creative-tutorials avatar Nov 15 '23 15:11 creative-tutorials

Switch to http://localhost and it'll work. Nothing was wrong with my code nor clerk dashboard configuration. Are you using another hostname and then reverse proxying to https://localhost ?

On Wed, Nov 15, 2023, 9:04 AM Timi @.***> wrote:

@creative-tutorials https://github.com/creative-tutorials turns out it was not a dependency issue at all. I finally figured out on my own that if you are using a hostname other than localhost over http you hit this redirect due https upgrade header. When I opened up the port on my container and accessed the app via localhost:3000 it worked as designed. I mentioned all of this in a support thread on discord. Honestly kind of dissappointed with the lack of support here from Clerk. I had to debug and figure this out, not one staff member even ones I reached out to directly helped me figure out what's going on. If there was FOSS I'd be totally okay with this, but a paid product...not a good look. A callout in the docs is all I've asked but it's fallen on deaf ears. Catching it via software and producing a warning instead of an infinite redirect would be better.

Hi @jameslporter https://github.com/jameslporter we are facing the exact same issue where our deployed container shows https://localhost:3000 in the Clerk debug info instead of the actual hostname, which fails with https. Did you find a workaround to make it work with an external hostname?

URL debug, { "url": "https://localhost:3000/", "method": "GET", "headers": "{...}", "nextUrl": "https://localhost:3000/", "clerkUrl": "https://localhost:3000/" }

I never changed much in my code, but I did change some things in my clerk dashboard tho

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

jameslporter avatar Nov 15 '23 15:11 jameslporter

thx @jameslporter , seems to be related to changes in Next v14, see my comment in #2115. It might be a recently introduced bug in Next 14 or an issue with how Clerk determines the current protocol and hostname.

stx-chris avatar Nov 15 '23 15:11 stx-chris