auth.api.getSession() returning null even when the cookies are being sent.

Hey devs, for some reason in a API route in NextJS im getting the current session but for some reason it comes out null .

export async function POST(request: NextRequest) {
  try {
    // Check authentication
    const session = await auth.api.getSession({ headers: await headers() });
    console.log(session) // this outputs `null`
    if (!session) {
        return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    // do something here if the user is authenticated
  } catch (error) {
    console.error("Error categorizing link:", error);
    return NextResponse.json(
      { error: "Failed to categorize link" },
      { status: 500 }
    );
  }
Was this page helpful?