nextjs-auth0: v4: Infinitely stacking cookies
Checklist
- The issue can be reproduced in the nextjs-auth0 sample app (or N/A).
- I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
- I have looked into the API documentation and have not found a suitable solution or answer.
- I have searched the issues and have not found a suitable solution or answer.
- I have searched the Auth0 Community forums and have not found a suitable solution or answer.
- I agree to the terms within the Auth0 Code of Conduct.
Description
- v4 sdk creates a new additional transactional cookie each time the unauthenticated user navigates to the app
- v4 logout does not remove said cookies
v3 does not create infinite cookies and does remove cookies on logout.
This eventually leads to a situation where the header of the request is too large.
Reproduction
- Be unauthenticated in your application (remove all cookies on that domain if you want)
- Navigate to a route in your application (receive a cookie)
- Navigate to another route in your application (receive another cookie)
- Repeat as many times as you wish
- Navigate to
/auth/logout(receive another cookie, and cookies are not removed)
https://github.com/user-attachments/assets/21076f33-3c81-428b-b84b-4d566883cb4e
Additional context
N/A
nextjs-auth0 version
4.0
Next.js version
15.1.6
Node.js version
20.x
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 15
- Comments: 18 (7 by maintainers)
Indeed - we have a migration branch and every time we deploy it we immediately start hitting 413s. We can manually wipe our cookies and things seem OK, but obviously we aren’t expecting our users to do this. This is a blocking problem on the migration path, @tusharpandey13 have you folks had any luck isolating the root cause here? We can’t upgrade nextJS until v4 of this SDK is working more reliably.
We had some user reports of this in production. Sharing this hacky fix until there is a patch available. This is a snippet from our middleware function:
On
/auth/loginit will delete any stale__txncookies before creating a new one. Important to get the existing cookies before you apply the auth0.middleware or else you will end up deleting the__txncookie for the current login attempt.Trying to migrate von v3 to v4 and immediately encountered this problem. Only had problems with the migration and now the app is breaking for users because of this. I wished this would be treated with a higher priority.
The unique transaction cookie solves the following issue:
This is solved by using a unique transaction cookie for each transaction that was started, but with its own consequences.
So it is important to know that once we would bring back a single transaction cookie in an opt-in way, you will be open for the above behavior again, which is also why I think it is important to find the actual root cause of the cookies stacking, and not the fact that they aren’t clearing, because I do think using a unique cookie per transaction should be the better configuration.
What I am curious to understand is what is causing these infinite stacking cookies, can you elaborate @jgeurts ? I see some inaccurate information in this thread, such as that the transaction cookie is not removed after logging in, which isn’t entirely accurate (see here), let me elaborate:
If the above is not happening, that is definitely a bug. But important in this case is to look at the single, unique cookie in isolation here.
What we do need to solve:
- If the login is completed with an error, and you are not logged in, we should delete the transaction cookie.
What does not happen is:
The above is expected because v4 supports multiple transactions, clearing any unrelated transaction cookies will break that functionality. Instead, we rely on the cookies expiring after 1 hour and self-cleaning after an hour. Are you saying this is not happening?
Based on my understanding, the transaction cookies can only stack infinitly if you:
Both are not expected scenario’s and the second one should be solved differently.
That said, that does not mean we want to better understand what is going on, and even improve where we can. Can you help us understand?
Some solutions I can think of is to add a configuration options to the SDK that allows either:
The latter won’t resolve the problem, but would reduce the timespan the cookies exist, reducing the chance for them to grow.
@tusharpandey13 do you have an ETA on a revised fix for this? We’re seeing random logout issues for some of our users recently which I suspect is related to v4 upgrade. Haven’t been able to identify a root cause but this seems like it could be a potential reason.
My team is experiencing this issue after upgrading to v4. We’re currently on ‘^4.6.1’ and using custom routes to keep the v3 paths of
/api/auth/*.I’ve added the middleware workaround outlined above but we’re still seeing 431s.
Maybe my middleware setup is bunk but I’m out of ideas.