Skip to content

Commit

Permalink
fix: add getSetCookie to session cookies if supported (#8631)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenstrom committed Sep 29, 2023
1 parent 37865b6 commit f2c23db
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions packages/next-auth/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ export function initAuth(config: NextAuthConfig) {
const auth = await authResponse.json()

// Preserve cookies set by Auth.js Core
const cookies = authResponse.headers.get("set-cookie")
if (cookies) response?.setHeader("set-cookie", cookies)
cloneSetCookie({ from: authResponse, to: response })

return auth satisfies AuthSession | null
})
Expand Down Expand Up @@ -192,14 +191,34 @@ async function handleAuth(
}
}

// Preserve cookies set by Auth.js Core
const finalResponse = new Response(response?.body, response)
const authCookies = sessionResponse.headers.get("set-cookie")
if (authCookies) finalResponse.headers.set("set-cookie", authCookies)
// Preserve cookies set by Auth.js Core
cloneSetCookie({ from: sessionResponse, to: finalResponse })

return finalResponse
}

/**
* Mutates `to` with the set-cookie from `from`
* Uses `getSetCookie` if available, otherwise falls back to `set-cookie`
*
* getSetCookie is missing from the types, but it's available on many platforms.
*/
function cloneSetCookie({ from, to }: { from: Response; to: Response }) {
const authCookies =
"getSetCookie" in from.headers
? (from.headers.getSetCookie as any)()
: from.headers.get("set-cookie")

if (!authCookies) return

if (Array.isArray(authCookies)) {
authCookies.forEach((cookie) => to.headers.append("set-cookie", cookie))
} else {
to.headers.set("set-cookie", authCookies)
}
}

/** Check if the request is for a NextAuth.js action. */
function isNextAuthAction(
req: NextRequest,
Expand Down

0 comments on commit f2c23db

Please sign in to comment.