Skip to content

Commit

Permalink
fix: show experimental api warning only in dev and only once (#4816)
Browse files Browse the repository at this point in the history
Co-authored-by: Lluis Agusti <hi@llu.lu>
  • Loading branch information
ndom91 and ubbe-xyz committed Jul 2, 2022
1 parent 163d8c6 commit 993c0f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/next-auth/src/next/index.ts
Expand Up @@ -83,20 +83,28 @@ function NextAuth(

export default NextAuth

let experimentalWarningShown = false
export async function unstable_getServerSession(
...args:
| [GetServerSidePropsContext['req'], GetServerSidePropsContext['res'], NextAuthOptions]
| [
GetServerSidePropsContext["req"],
GetServerSidePropsContext["res"],
NextAuthOptions
]
| [NextApiRequest, NextApiResponse, NextAuthOptions]
): Promise<Session | null> {
console.warn(
"[next-auth][warn][EXPERIMENTAL_API]",
"\n`unstable_getServerSession` is experimental and may be removed or changed in the future, as the name suggested.",
`\nhttps://next-auth.js.org/configuration/nextjs#unstable_getServerSession}`,
`\nhttps://next-auth.js.org/warnings#EXPERIMENTAL_API`
if (!experimentalWarningShown && process.env.NODE_ENV !== "production") {
console.warn(
"[next-auth][warn][EXPERIMENTAL_API]",
"\n`unstable_getServerSession` is experimental and may be removed or changed in the future, as the name suggested.",
`\nhttps://next-auth.js.org/configuration/nextjs#unstable_getServerSession}`,
`\nhttps://next-auth.js.org/warnings#EXPERIMENTAL_API`
)
experimentalWarningShown = true
}

const [req, res, options] = args

const [req, res, options] = args;

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

const session = await NextAuthHandler<Session | {}>({
Expand Down
15 changes: 15 additions & 0 deletions packages/next-auth/tests/getServerSession.test.ts
Expand Up @@ -50,4 +50,19 @@ describe("Treat secret correctly", () => {
expect(logger.error).toBeCalledTimes(1)
expect(logger.error).toBeCalledWith("NO_SECRET", expect.any(MissingSecret))
})

it("Only logs warning once and in development", async () => {
// Expect console.warn to NOT be called due to NODE_ENV=production
await unstable_getServerSession(req, res, { providers: [], logger })
expect(console.warn).toBeCalledTimes(0)

// Expect console.warn to be called ONCE due to NODE_ENV=development
process.env.NODE_ENV = "development"
await unstable_getServerSession(req, res, { providers: [], logger })
expect(console.warn).toBeCalledTimes(1)

// Expect console.warn to be still only be called ONCE
await unstable_getServerSession(req, res, { providers: [], logger })
expect(console.warn).toBeCalledTimes(1)
})
})

0 comments on commit 993c0f4

Please sign in to comment.