@@ -9,6 +9,7 @@ import type {
9
9
} from "next"
10
10
import type { NextAuthOptions , Session } from ".."
11
11
import type {
12
+ CallbacksOptions ,
12
13
NextAuthAction ,
13
14
NextAuthRequest ,
14
15
NextAuthResponse ,
@@ -90,17 +91,25 @@ export default NextAuth
90
91
91
92
let experimentalWarningShown = false
92
93
let experimentalRSCWarningShown = false
93
- export async function unstable_getServerSession (
94
+
95
+ type GetServerSessionOptions = Partial < Omit < NextAuthOptions , "callbacks" > > & {
96
+ callbacks ?: Omit < NextAuthOptions [ 'callbacks' ] , "session" > & {
97
+ session ?: ( ...args : Parameters < CallbacksOptions [ "session" ] > ) => any
98
+ }
99
+ }
100
+
101
+ export async function unstable_getServerSession <
102
+ O extends GetServerSessionOptions ,
103
+ R = O [ "callbacks" ] extends { session : ( ...args : any [ ] ) => infer U }
104
+ ? U
105
+ : Session
106
+ > (
94
107
...args :
95
- | [
96
- GetServerSidePropsContext [ "req" ] ,
97
- GetServerSidePropsContext [ "res" ] ,
98
- NextAuthOptions
99
- ]
100
- | [ NextApiRequest , NextApiResponse , NextAuthOptions ]
101
- | [ NextAuthOptions ]
108
+ | [ GetServerSidePropsContext [ "req" ] , GetServerSidePropsContext [ "res" ] , O ]
109
+ | [ NextApiRequest , NextApiResponse , O ]
110
+ | [ O ]
102
111
| [ ]
103
- ) : Promise < Session | null > {
112
+ ) : Promise < R | null > {
104
113
if ( ! experimentalWarningShown && process . env . NODE_ENV !== "production" ) {
105
114
console . warn (
106
115
"[next-auth][warn][EXPERIMENTAL_API]" ,
@@ -128,7 +137,8 @@ export async function unstable_getServerSession(
128
137
129
138
let req , res , options : NextAuthOptions
130
139
if ( isRSC ) {
131
- options = args [ 0 ] ?? { providers : [ ] }
140
+ options = Object . assign ( { } , args [ 0 ] , { providers : [ ] } )
141
+
132
142
// eslint-disable-next-line @typescript-eslint/no-var-requires
133
143
const { headers, cookies } = require ( "next/headers" )
134
144
req = {
@@ -143,7 +153,7 @@ export async function unstable_getServerSession(
143
153
} else {
144
154
req = args [ 0 ]
145
155
res = args [ 1 ]
146
- options = args [ 2 ]
156
+ options = Object . assign ( args [ 2 ] , { providers : [ ] } )
147
157
}
148
158
149
159
options . secret ??= process . env . NEXTAUTH_SECRET
@@ -173,7 +183,7 @@ export async function unstable_getServerSession(
173
183
if ( status === 200 ) {
174
184
// @ts -expect-error
175
185
if ( isRSC ) delete body . expires
176
- return body as Session
186
+ return body as R
177
187
}
178
188
throw new Error ( ( body as any ) . message )
179
189
}
0 commit comments