session null in nextjs route handler

I know auth and sessions is working because it I can log in and out with no problem. My server functions are able to get sessions but whenever I try and get the session from a route handler it's null This same function is used throughout my app and works everywhere except route handlers
const session = await auth.api.getSession({
headers: await headers(),
});

if (!session) {
return {
user: null,
session: null,
};
}

return {
session: session.session,
user: session.user,
};
const session = await auth.api.getSession({
headers: await headers(),
});

if (!session) {
return {
user: null,
session: null,
};
}

return {
session: session.session,
user: session.user,
};
4 Replies
Shaki
ShakiOP4mo ago
I found the fix. You have to pass the headers to the api call inside of your server component
awexis
awexis2mo ago
Can you show how you passed the headers please. Is it just doing the same await headers and then passing that to the api call in your server component?
Shaki
ShakiOP2mo ago
sorry just seeing this. When I call the api inside of a server action I just pass the headers
await auth.api.createOrganization({
body: {
name: input.name,
keepCurrentActiveOrganization: true,
description: input.description,
},
headers: await headers(),
});
await auth.api.createOrganization({
body: {
name: input.name,
keepCurrentActiveOrganization: true,
description: input.description,
},
headers: await headers(),
});
awexis
awexis2mo ago
Yeah that is what I ended up doing and it worked as well. Thank you so much

Did you find this page helpful?