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

Nope I'm on 14 as well. It's not a code problem. It's how you are running it. Again the only way I've gotten it to work properly for http localdev is to use localhost:3000. Getting kind of frustrated with the lack of support here. I feel like I'm the clerk support staff trying to help others through the problems I experienced. I really just want confirmation that my hypothesis is correct and if so this be stated clearly in the documentation. My requests have fallen on deaf ears.

On Wed, Nov 15, 2023 at 9:30 AM stx-chris @.***> wrote:

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

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

jameslporter avatar Nov 15 '23 15:11 jameslporter

Ok in that case our issues are different, because local development and build work fine and we don't get any token-related issues. It's the same with Next v13 and v14.

But once we build our Docker image and upload it to Google Cloud, with the only difference being the Next.js version, Clerk's url debug value is http://localhost:3000 within the Docker container on v13 and https://localhost:3000 on v14 and the error reason we receive is cross-origin-referrer, which is expected in that case.

Unfortunately, there is no way we can fix how Next.js determines its nextUrl, but the SSL protocol is definitely wrong.

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

We have received one reproduction for Next 14 specific redirect issues. Engineering is looking st this. We encourage the creation of reproductions of your issue where and when possible, and posting those in https://github.com/clerk/javascript/issues/2115.

royanger avatar Nov 15 '23 15:11 royanger

If only the people who knew the answer were here to help. Godspeed and good luck! Maybe if you share more about your docker setup we can find out what isn't quite right.

On Wed, Nov 15, 2023, 9:47 AM stx-chris @.***> wrote:

Ok in that case our issues are different, because local development and build work fine and we don't get any token-related issues. It's the same with Next v13 and v14.

But once we build our Docker image and upload it to Google Cloud, with the only difference being the Next.js version, Clerk's url debug value is http://localhost:3000 within the Docker container on v13 and https://localhost:3000 on v14 and the error reason we receive is cross-origin-referrer, which is expected in that case.

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

jameslporter avatar Nov 15 '23 15:11 jameslporter

@stx-chris @creative-tutorials @jameslporter After digging heavily into Next's internals, as well as performing a number of tests with SSL termination and reverse proxying, I'm fairly confident that the PR we submitted to Next.js earlier today, will solve these issues.

Currently, the PR is approved and, once merged, the changes will be available as a canary build of v14.0.3.

In the meantime, you should should be able to mitigate these issues by setting the X-Forwarded-Host request header to the Host header via Varnish, or otherwise.

This was my configuration via Caddy:

localhost {
  reverse_proxy localhost:3000 {
    header_up X-Forwarded-Host {host}
  }
}

In short, if X-Forwarded-Host is already set, Next doesn't overwrite it with the incorrect value.

tmilewski avatar Nov 16 '23 08:11 tmilewski

Awesome thanks for the update. I'm curious what kind of integration tests do you all have around clerk/nextjs and how do you go about testing new next releases to ensure regressions like this don't happen?

jameslporter avatar Nov 16 '23 12: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 @creative-tutorials @jameslporter After digging heavily into Next's internals, as well as performing a number of tests with SSL termination and reverse proxying, I'm fairly confident that the PR we submitted to Next.js earlier today, will solve these issues.

Currently, the PR is approved and, once merged, the changes will be available as a canary build of v14.0.3.

In the meantime, you should should be able to mitigate these issues by setting the X-Forwarded-Host request header to the Host header via Varnish, or otherwise.

This was my configuration via Caddy:

localhost {
  reverse_proxy localhost:3000 {
    header_up X-Forwarded-Host {host}
  }
}

In short, if X-Forwarded-Host is already set, Next doesn't overwrite it with the incorrect value.

Thanks for the update

creative-tutorials avatar Nov 16 '23 14:11 creative-tutorials

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

Thanks, It worked for me. i was on node v21, switched back to 18 and it's working

webstar-code avatar Nov 28 '23 16:11 webstar-code

I still have this issue. I have cleared cache and cookies, I have gone from node v21 to v18, I have changed date and time on and off. I am using localhost:3000

harryparkinson101 avatar Nov 28 '23 17:11 harryparkinson101

I still have the issue as well on node 18. Works on localhost directly to the service. Doesn't work through my varnish front proxy that maps the hostnames to service backend host. The same varnish setup works in stage/prod as it's using https via AWS elb with certificate manager. I sent my har files to Colin.

jameslporter avatar Nov 28 '23 17:11 jameslporter

I had same issue but I solved it.

The following is what we did this time.

  • switch to node v18.18.2 (current latest version)
  • install latest version of @clerk/nextjs
  • update packages.

Naokiakazawa avatar Dec 04 '23 09:12 Naokiakazawa

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

Ontorok avatar Dec 26 '23 04:12 Ontorok

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.

creative-tutorials avatar Dec 27 '23 20:12 creative-tutorials

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

amateurbeekeeper avatar Jan 07 '24 14:01 amateurbeekeeper

For me the underlying problem is that clerk doesn't support multiple instances on the same domain. I had a subdomain per environment. They say they are working on removing this limitation but unclear when.

On Sun, Jan 7, 2024, 8:59 AM amateurbeekeeper @.***> wrote:

hmmm still broken on my end

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

jameslporter avatar Jan 07 '24 15:01 jameslporter

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)

stx-chris avatar Jan 15 '24 19:01 stx-chris

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!

carlelgstroem avatar Jan 22 '24 00:01 carlelgstroem

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.

creative-tutorials avatar Jan 26 '24 11:01 creative-tutorials

I'm running into this on node v21.6.1

eraoul avatar Jan 29 '24 04:01 eraoul

I'm running into this on node v21.6.1

Try syncing your clock please

creative-tutorials avatar Jan 29 '24 12:01 creative-tutorials