Authentication state is lost when JWT is split into multiple chunks #725
Labels
bug
A bug that needs to be resolved
p4
Important Issue
provider-authjs
An issue with the authjs provider
Environment
Reproduction
sign in (credentials)
Describe the bug
What is happening
I found that authentication state is lost on browser reload when the JWT is too long to fit in a cookie limit.
next-auth
splits a JWT token into multiple cookies when it is too long to fit in a single cookie.When I signin the following seems to occur.
Browser sends a request to
/api/auth/callback/credentials
and the server returns a response with JWT token set to multiple Set-Cookie headers.Browser sends a request
/
and server responds with only oneSet-Cookie
header. But when carefully observed there are multiple cookies split by,
as its value.But browsers assumes text followed by
,
as attribute for the first cookie. As you can see it the image below, the browser interpreted that there is only first chunk. And this makes cookies for JWT corrupted, which leads to unauthenticated state, I suppose.Why are multiple cookies merged into one
Set-Cookie
?During SSR the server calls session API to fetch session info, it then forwards Set-Cookie headers obtained from the API to the browser.
When extracting Set-Cookie headers from the API the server uses
Headers.get("set-cookie")
, and it seems this method joins multiple Set-Cookie into one Set-Cookie.Possible solution
Instead of using
Headers.get
we can use Headers.getSetCookie. It returns an array of cookie values.Related: #293
Additional context
No response
Logs
The text was updated successfully, but these errors were encountered: