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 }
);
}
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 }
);
}
2 Replies
shadow
shadowOP4w ago
As you can see the headers are being sent
No description
shadow
shadowOP4w ago
Am i missing something here? But inside the api route, request.headers doesnt contain the cookie

Did you find this page helpful?