TypeError: res.getHeader is not a function in NextJS API
Asked Answered
C

3

5

I am trying to create a web application with NextJS 13 with TypeScript, Next Auth v4, Prisma (with a SQLite database for development) and OpenAI.

The console displays an error whenever I access to the API endpoint with the following message:

error - TypeError: res.getHeader is not a function

at setCookie (webpack-internal:///(sc_server)/./node_modules/next-auth/next/utils.js:11:49)

I have detected that the code fails whenever it tries to assign the session variable with getServerSession() at this line:

export async function GET(
    req: NextApiRequest, 
    res: NextApiResponse
) {
    const session = await getServerSession(req, res, authOptions) // <-- here it fails
    const sessionErrors = checkSessionErrors(session, req.url);
    if (sessionErrors) return sessionErrors;

    // rest of the code
}

I have been researching over the internet and I found some issues related to a Webpack middleware. But I am not using any middleware.

I have tried also replacing the code to look like this:

export async function GET(req: NextRequest) {
    const session = await getServerSession() // not sure how to fill this now
    const sessionErrors = checkSessionErrors(session, req.url);
    if (sessionErrors) return sessionErrors;

    // rest of the code
    // return stuff with NextResponse.json()
}

Not sure how to proceed now, any suggested actions on this?

Clutter answered 24/5, 2023 at 3:14 Comment(0)
R
12

getServerSession expects NextApiRequest and NextApiResponse arguments. I solved this by adding the getHeader and setHeader functions to NextResponse:

export async function POST(req: NextRequest, res: NextResponse) {
  const session = await getServerSession(
    req as unknown as NextApiRequest,
    {
      ...res,
      getHeader: (name: string) => res.headers?.get(name),
      setHeader: (name: string, value: string) => res.headers?.set(name, value),
    } as unknown as NextApiResponse,
    authOptions
  );
}


Rohrer answered 24/5, 2023 at 8:39 Comment(1)
this is not working for me can you explain the same a little more, please?Bogie
C
9

You do not need to pass the req & res in when using the app router. it works with just the authOptions.

Caterinacatering answered 28/6, 2023 at 10:0 Comment(4)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Clipped
Despite its brevity, this is what worked for me.Apopemptic
Jon, can you explain your fix way in brief please?Bogie
You can just use const session = await getServerSession(authOptions);, similar to Ethan's answerStilliform
C
3

According to Ben's answer, I gave it a try and found that it works like a charm. Therefore, I am sharing my code:

// next: 14.0.3
// next-auth: 4.24.5

// Change the path to where your `authOptions` is
import { authOptions } from '@/utils/auth'

import { getServerSession } from 'next-auth'

export async function POST() {
  const session = await getServerSession(authOptions)
  if (!session) {
    return Response.json({ message: 'You must be logged in.' }, { status: 401 })
  }

  try {
    const result = someOperations() // ...

    return Response.json(result)
  } catch (err) {
    return Response.json(err, { status: 500 })
  }
}

Here are the relevant docs:

  1. NextJS Route Handlers
  2. NextAuth getServerSession

Updated on 2023/12/17:

I found I've made a mistake. We can't export authOptions from a route file because of NextJS's rule. Therefore, I modified the code and changed the import path to @/utils/auth.

Chaisson answered 14/12, 2023 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.