Cant seem to get cookies using new api routing
Unanswered
Giant ichneumon wasp posted this in #help-forum
Giant ichneumon waspOP
Hello,
This is how I used to get my cookies using pages/api:
However, I can't get any cookies using the new app directory api routes:
Am I doing something wrong here?
This is how I used to get my cookies using pages/api:
// everything works here ✅
const { affiliate } = req.cookies;However, I can't get any cookies using the new app directory api routes:
// affiliate is undefined âŒ
import { NextResponse } from 'next/server';
import { cookies } from 'next/headers';
export async function POST(request: Request) {
const cookieStore = cookies();
const affiliate = cookieStore.get('affiliate');
return NextResponse.json({
affiliate,
});
}Am I doing something wrong here?
9 Replies
Giant panda
How do you call that route handler? From client or server-side code?
American Curl
Instead of
Request, try using NextRequest. Import it from next/server like you did for your responseGiant panda
That's unrelated.
If OP is calling the route from the server then even the abstration on top of Request will return no cookies.
@Giant panda How do you call that route handler? From client or server-side code?
Giant ichneumon waspOP
Its from the client, I'm calling the route like this:
and i tried to get cookies like this
but it seems like im only getting the auth token, instead of all of my cookies?
const result = await axios.post('/api/get-user');and i tried to get cookies like this
import { NextRequest, NextResponse } from 'next/server';
export async function POST(request: NextRequest) {
const cookies = request.cookies;
const allcookies = cookies.getAll();
return NextResponse.json({
allcookies,
});
}but it seems like im only getting the auth token, instead of all of my cookies?
Giant panda
Well then, then you shoudl have the same behavior with the
cookies() function and there is technically nothing broken. Most likely you cannot access your affiliate cookie due to SameSite policy mismatches or similar.Giant panda
For one, you have to check whether the host and path in addition to its policy allow your Next backend to receive it.
@Giant panda For one, you have to check whether the host and path in addition to its policy allow your Next backend to receive it.
Giant ichneumon waspOP
Hey sorry, i just tried the exact same code for my other project and everything worked, I think the only difference is that I am using different version of supabse/auth-helpers, do you think that could be the reason why cookies object is completely different?