Skip to content

Commit

Permalink
fix: return null in unstable_getServerSession if there's an error (#5218
Browse files Browse the repository at this point in the history
)

* fix: return null in unstable_getServerSession if there's an error

* Remove status check and instead check body is not a string

* Combine similar tests
  • Loading branch information
melanieseltzer committed Aug 31, 2022
1 parent 24db833 commit a82cbf5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/next-auth/src/next/index.ts
Expand Up @@ -107,7 +107,7 @@ export async function unstable_getServerSession(

options.secret = options.secret ?? process.env.NEXTAUTH_SECRET

const session = await NextAuthHandler<Session | {}>({
const session = await NextAuthHandler<Session | {} | string>({
options,
req: {
host: detectHost(req.headers["x-forwarded-host"]),
Expand All @@ -122,7 +122,9 @@ export async function unstable_getServerSession(

cookies?.forEach((cookie) => setCookie(res, cookie))

if (body && Object.keys(body).length) return body as Session
if (body && typeof body !== "string" && Object.keys(body).length)
return body as Session

return null
}

Expand Down
57 changes: 53 additions & 4 deletions packages/next-auth/tests/getServerSession.test.ts
@@ -1,10 +1,14 @@
import * as core from "../src/core"
import { MissingSecret } from "../src/core/errors"
import { unstable_getServerSession } from "../src/next"
import { mockLogger } from "./lib"

let originalWarn = console.warn
let logger = mockLogger()

const req: any = { headers: {} }
const res: any = { setHeader: jest.fn(), getHeader: jest.fn() }

beforeEach(() => {
process.env.NODE_ENV = "production"
process.env.NEXTAUTH_URL = "http://localhost"
Expand All @@ -19,9 +23,6 @@ afterEach(() => {
})

describe("Treat secret correctly", () => {
const req: any = { headers: {} }
const res: any = { setHeader: jest.fn(), getHeader: jest.fn() }

it("Read from NEXTAUTH_SECRET", async () => {
process.env.NEXTAUTH_SECRET = "secret"
await unstable_getServerSession(req, res, { providers: [], logger })
Expand All @@ -44,8 +45,12 @@ describe("Treat secret correctly", () => {
})

it("Error if missing NEXTAUTH_SECRET and secret", async () => {
await unstable_getServerSession(req, res, { providers: [], logger })
const session = await unstable_getServerSession(req, res, {
providers: [],
logger,
})

expect(session).toEqual(null)
expect(logger.error).toBeCalledTimes(1)
expect(logger.error).toBeCalledWith("NO_SECRET", expect.any(MissingSecret))
})
Expand All @@ -65,3 +70,47 @@ describe("Treat secret correctly", () => {
expect(console.warn).toBeCalledTimes(1)
})
})

describe("Return correct data", () => {
afterEach(() => {
jest.restoreAllMocks()
})

it("Should return null if there is no session", async () => {
const spy = jest.spyOn(core, "NextAuthHandler")
spy.mockReturnValue({ body: {} })

const session = await unstable_getServerSession(req, res, {
providers: [],
logger,
secret: "secret",
})

expect(session).toEqual(null)
})

it("Should return the session if one is found", async () => {
const mockedResponse = {
body: {
user: {
name: "John Doe",
email: "test@example.com",
image: "",
id: "1234",
},
expires: "",
},
}

const spy = jest.spyOn(core, "NextAuthHandler")
spy.mockReturnValue(mockedResponse)

const session = await unstable_getServerSession(req, res, {
providers: [],
logger,
secret: "secret",
})

expect(session).toEqual(mockedResponse.body)
})
})

0 comments on commit a82cbf5

Please sign in to comment.