MCPcopy
hub / github.com/nextauthjs/next-auth / getServerSession

Function getServerSession

packages/next-auth/src/next/index.ts:177–230  ·  view source on GitHub ↗
(...args: GetServerSessionParams<O>)

Source from the content-addressed store, hash-verified

175 | []
176
177export async function getServerSession<
178 O extends GetServerSessionOptions,
179 R = O["callbacks"] extends { session: (...args: any[]) => infer U }
180 ? U
181 : Session
182>(...args: GetServerSessionParams<O>): Promise<R | null> {
183 const isRSC = args.length === 0 || args.length === 1
184
185 let req, res, options: AuthOptions
186 if (isRSC) {
187 options = Object.assign({}, args[0], { providers: [] })
188
189 // eslint-disable-next-line @typescript-eslint/no-var-requires
190 const { headers, cookies } = require("next/headers")
191 req = {
192 headers: Object.fromEntries((await headers()) as Headers),
193 cookies: Object.fromEntries(
194 (await cookies()).getAll().map((c) => [c.name, c.value])
195 ),
196 }
197 res = { getHeader() {}, setCookie() {}, setHeader() {} }
198 } else {
199 req = args[0]
200 res = args[1]
201 options = Object.assign({}, args[2], { providers: [] })
202 }
203
204 options.secret ??= process.env.NEXTAUTH_SECRET ?? process.env.AUTH_SECRET
205
206 const session = await AuthHandler<Session | {} | string>({
207 options,
208 req: {
209 action: "session",
210 method: "GET",
211 cookies: req.cookies,
212 headers: req.headers,
213 },
214 })
215
216 const { body, cookies, status = 200 } = session
217
218 cookies?.forEach((cookie) => setCookie(res, cookie))
219
220 if (body && typeof body !== "string" && Object.keys(body).length) {
221 if (status === 200) {
222 // @ts-expect-error
223 if (isRSC) delete body.expires
224 return body as R
225 }
226 throw new Error((body as any).message)
227 }
228
229 return null
230}
231
232let deprecatedWarningShown = false
233

Callers 13

getServerSidePropsFunction · 0.90
handlerFunction · 0.90
handlerFunction · 0.90
protected-ssr.jsFile · 0.90
PageFunction · 0.90
getServerSidePropsFunction · 0.90
supabase-ssr.jsFile · 0.90
protected.jsFile · 0.90
session.jsFile · 0.90
supabase-rls.jsFile · 0.90
PageFunction · 0.90

Calls 1

setCookieFunction · 0.90

Tested by

no test coverage detected