Skip to content

Commit 782812a

Browse files
authoredNov 7, 2022
fix(next): correctly parse headers with RSC (#5753)
* fix(next): correctly parse headers with RSC * chore(dev): simplify/fix dev app * make authOptions optional for RSC case
1 parent 32f2a0c commit 782812a

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed
 
+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { unstable_getServerSession } from "next-auth/next"
2-
import { authOptions } from "pages/api/auth/[...nextauth]"
32

43
export default async function Page() {
5-
const session = await unstable_getServerSession(authOptions)
4+
const session = await unstable_getServerSession()
65
return <pre>{JSON.stringify(session, null, 2)}</pre>
76
}

‎apps/dev/pages/api/examples/session.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { unstable_getServerSession } from "next-auth/next"
33
import { authOptions } from "../auth/[...nextauth]"
44

55
export default async (req, res) => {
6-
const session = await unstable_getServerSession(authOptions)
6+
const session = await unstable_getServerSession(req, res, authOptions)
77
res.json(session)
88
}

‎packages/next-auth/src/next/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export async function unstable_getServerSession(
9494
]
9595
| [NextApiRequest, NextApiResponse, NextAuthOptions]
9696
| [NextAuthOptions]
97+
| []
9798
): Promise<Session | null> {
9899
if (!experimentalWarningShown && process.env.NODE_ENV !== "production") {
99100
console.warn(
@@ -105,7 +106,7 @@ export async function unstable_getServerSession(
105106
experimentalWarningShown = true
106107
}
107108

108-
const isRSC = args.length === 1
109+
const isRSC = args.length === 0 || args.length === 1
109110
if (
110111
!experimentalRSCWarningShown &&
111112
isRSC &&
@@ -122,11 +123,11 @@ export async function unstable_getServerSession(
122123

123124
let req, res, options: NextAuthOptions
124125
if (isRSC) {
125-
options = args[0]
126+
options = args[0] ?? { providers: [] }
126127
// eslint-disable-next-line @typescript-eslint/no-var-requires
127128
const { headers, cookies } = require("next/headers")
128129
req = {
129-
headers,
130+
headers: Object.fromEntries(headers() as Headers),
130131
cookies: Object.fromEntries(
131132
cookies()
132133
.getAll()

0 commit comments

Comments
 (0)
Please sign in to comment.