Skip to content

Commit

Permalink
fix: return null when throw on Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
devjiwonchoi committed Jun 11, 2024
1 parent b420d36 commit 23a3f91
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/next-auth/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,15 @@ export function initAuth(
return (...args: WithAuthArgs) => {
if (!args.length) {
// React Server Components
return getSession(headers(), config).then((r) => r.json())
return getSession(headers(), config)
.then((r) => r.json())
.then((data) => {
// error message from core Auth
// e.g. { message: "There was a problem with the server configuration..." }
if (data.message) return null
return data
})

}
if (args[0] instanceof Request) {
// middleware.ts inline
Expand Down
11 changes: 11 additions & 0 deletions packages/next-auth/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,14 @@ describe("signIn action", () => {
})
})
})

describe("auth action", () => {
it("should return null on session when throws", async () => {
let p = "should not be visible"
const session = await NextAuth({ providers: [] }).auth()
if (!session) p = "login"

expect(session).toEqual(null)
expect(p).toEqual("login")
})
})

0 comments on commit 23a3f91

Please sign in to comment.