KindeK
Kinde2y ago
5 replies
Albert

[NextJS 14] User session returns null in API route, when user is already authenticated from caller

Trying to understand what I'm doing wrong here:

I have a component that calls an api route on a button click, this component doesn't render unless the user is authenticated. I'm using the standard { getUser } = getKindeServerSession() and user = await getUser() methods to verify this. However, when I call the api route from the button click, these same methods seem to return null for me in the API route handler, the code I have so far is pretty simple:

export const POST = async (req: NextRequest) => {

const { getUser } = getKindeServerSession();
const user = await getUser();
const res = await req.json()

if(!user) {
console.log("no user")
}

It seems that I'm consisting hitting the "if(!user)" line - I'm wondering if there's any gotcha's that I should be aware of that could be causing this?
Was this page helpful?