During logout, both a blank and non-blank __Secure-authjs.session-token
Set-Cookie header are sent
#12907
Labels
bug
Something isn't working
invalid reproduction
The issue did not have a detectable valid reproduction URL
triage
Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Environment
(The environment from https://github.com/nextauthjs/next-auth-example)
Reproduction URL
https://next-auth-example.vercel.app/
Describe the issue
During the logout process, next-auth sends a
Set-Cookie
header to the client to inform the browser to clear the__Secure-authjs.session-token
cookie. Unfortunately, it ALSO sends a secondSet-Cookie
header that revalidates the cookie. See the screenshot below (repro steps below), you can see the response includes twoSet-Cookie
headers:__Secure-authjs.session-token=<encrypted token>; Path=/; Expires=Sun, 25 May 2025 22:22:36 GMT; HttpOnly; Secure; SameSite=Lax
__Secure-authjs.session-token=; Path=/; Max-Age=0; Secure; HttpOnly; SameSite=lax
The problem is that this is undefined behavior, according to https://www.rfc-editor.org/rfc/rfc6265#section-4.1.1, in particular "Servers SHOULD NOT include more than one Set-Cookie header field in the same response with the same cookie-name."
For some server runtimes, such as vercel and node, the blank cookie appears second, while on others, such as cloudflare workers and netlify, the blank cookie appears first. The outcome is that, at least on Chrome, the browser chooses to use the LAST header. So, if you use next-auth on cloudflare or netlify, the non-blank cookie is second, and thus the user is never signed out.
I found this question on a netlify help forum and this pointed me in the right direction.
https://answers.netlify.com/t/next-auth-session-not-clearing/119104/13
How to reproduce
Expected behavior
The solution I would like to see is that next-auth only sends the blank cookie on logout. I wonder if the non-blank cookie is coming from middleware that is automatically refreshing the cookie on every request?
The text was updated successfully, but these errors were encountered: