#Cant seem to get cookies using new api routing

10 messages · Page 1 of 1 (latest)

steady bay
#

Hello,
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?

wraith palmBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
wise cape
#

How do you call that route handler? From client or server-side code?

heady knoll
#

Instead of Request, try using NextRequest. Import it from next/server like you did for your response

wise cape
#

That's unrelated.

#

If OP is calling the route from the server then even the abstration on top of Request will return no cookies.

steady bay
# wise cape How do you call that route handler? From client or server-side code?

Its from the client, I'm calling the route like this:

    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?

wise cape
#

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.

steady bay
wise cape
#

For one, you have to check whether the host and path in addition to its policy allow your Next backend to receive it.