nextauthjs / nextauthjs/next-auth
`getServerSession`, if called more than once, generate and appends multiple `next-auth.session_token` to `set-cookie` header to response object
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 28.4k
- Forks
- 4k
- PR merge metrics
- No merged PRs in 30d
Description
Environment
next 12.1.5
react 18.0.0
next-auth 4.20.1
Reproduction URL
Describe the issue
I have an API route that invokes getServerSession more than once. It's response header set-cookie was bloated with next-auth.session_token to the point HPE_HEADER_OVERFLOW was throw in proxy server.
const handler: NextApiHandler = async (req, res) => {
const data1 = await fetchData1(req, res)
const data2 = await fetchData2(req, res)
}
const fetchData1 = async(req, res) => {
await getServerSession(req, res, conf)
...
}
const fetchData2 = async(req, res) => {
await getServerSession(req, res, conf)
...
}

How to reproduce
Expected behavior
Consider that method name is "getServerSession", multiple calls should still generate and set to header only once (would be better if we can detect and re-use previous call result)
Code in question
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at packages/next-auth/src/next/index.ts lines 210-222 and inspect how repeated getServerSession calls update the response headers. Reproduce the issue with an API route that calls getServerSession twice, then verify that the response contains only one next-auth.session_token cookie and does not accumulate headers.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- next.js, typescript
- Domain
- api, authentication
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100