NextAuth in Next14 app route
Answered
English Angora posted this in #help-forum
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".
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 })
}
...
}2 Replies
@English Angora 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".
export const POST = (req: NextRequest) => {
const session = await auth()
if (!session) {
return Response.json({}, { status: 400 })
}
...
}Answer