Next.js Discord

Discord Forum

NextAuth in Next14 app route

Answered
English Angora posted this in #help-forum
Open in Discord
English AngoraOP
Do you have any example of using Next.js 14 with NextAuth in a protected API route (app folder) and reading the session there? I'm encountering type errors during the build.

Type error: Route "app/api/grati/xxx/route.tsx" has an invalid export:
"unknown" is not a valid POST return type:
Expected "void | Response | Promise<void | Response>", got "unknown".
Answered by Ray
export const POST = (req: NextRequest) => {
  const session = await auth()
  if (!session) {
    return Response.json({}, { status: 400 })
  }

  ...
}
View full answer

2 Replies