Next.js Discord

Discord Forum

getServerSession returning null on Vercel deploy

Unanswered
Orinoco Crocodile posted this in #help-forum
Open in Discord
Orinoco CrocodileOP
I am building an app that uses NextAuth for authentication. I am using the app router with Next.js. I have a route handler in which I am relying on a getServerSession call to authenticate the user before effectuating an update operation to the database. This works just fine locally. It returns the session for the authenticated user and then, the update operation runs smoothly. However, in the Vercel deploy, getServerSession returns null. Does anyone know why this is happening?

I have read through numerous posts where others have run into the same issue. However, none of the stated solutions have worked for me. It's worth noting that the getSession calls I am making work as expected both locally and deployed.

Here is the actual route handler:

// CREATE ORDER
export async function POST(req: NextRequest) {
  // const session = await getAuthSession();
  const session = await getServerSession(authOptions);
  console.log("session: ", session);
  const token = await getToken({ req, secret });
  console.log("token: ", token);

  if (session) {
    try {
      const body = await req.json();

      const order = await prisma.order.create({ data: body });
      return new NextResponse(JSON.stringify(order), { status: 201 });
    } catch (error) {
      console.log(error);
      return new NextResponse(
        JSON.stringify({ message: "A server error has occurred" }),
        { status: 500 }
      );
    }
  } else {
    return new NextResponse(
      JSON.stringify({ message: "You are not authenticated!" }),
      { status: 401 }
    );
  }
}

4 Replies

@Orinoco Crocodile I am building an app that uses NextAuth for authentication. I am using the app router with Next.js. I have a route handler in which I am relying on a `getServerSession` call to authenticate the user before effectuating an update operation to the database. This works just fine locally. It returns the session for the authenticated user and then, the update operation runs smoothly. However, in the Vercel deploy, `getServerSession` returns null. Does anyone know why this is happening? I have read through numerous posts where others have run into the same issue. However, none of the stated solutions have worked for me. It's worth noting that the `getSession` calls I am making work as expected both locally and deployed. Here is the actual route handler: // CREATE ORDER export async function POST(req: NextRequest) { // const session = await getAuthSession(); const session = await getServerSession(authOptions); console.log("session: ", session); const token = await getToken({ req, secret }); console.log("token: ", token); if (session) { try { const body = await req.json(); const order = await prisma.order.create({ data: body }); return new NextResponse(JSON.stringify(order), { status: 201 }); } catch (error) { console.log(error); return new NextResponse( JSON.stringify({ message: "A server error has occurred" }), { status: 500 } ); } } else { return new NextResponse( JSON.stringify({ message: "You are not authenticated!" }), { status: 401 } ); } }
Gull Dong
Is secret defined? Don't forget to use process.env
and if you did use process.env to define the secret then make sure all your variables in vercels .env section r correct
Orinoco CrocodileOP
Hey @Gull Dong. So it wasn't the .env variables. The problem was that I was not calling the route handler from relative path. If you use the full domain (https://<the_domain>/api/orders), as opposed to just /api/orders, then it doesn't work
Gull Dong
least you figured it out :)