Next.js Discord

Discord Forum

Cookie passing

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
Hi guys i am facing problem i can't handle it. please help me if u can

I have project next + nest my next run at http://localhost:3001 and my nest on http://localhost:3010 and when i sending request to my nest from next i can't have cookies i am wondering why?!

This is my code in nestjs:

export class MyAuthGuard extends BaseSupabaseAuthGuard {
  constructor(supabaseClient: SupabaseClient) {
    super(supabaseClient);
  }

  protected extractTokenFromRequest(request: Request): string | undefined {
    const cookies = request.cookies;
    console.log(cookies);
  }
}


and every time i get null value.

i read somewhere that i must change my main.ts to this:

const app = await NestFactory.create(AppModule);
  app.use(cookieParser());
  app.enableCors({
    origin: 'http://localhost:3001',
    credentials: true,
  });
  await app.listen(3010);


and i wanna mention i done it but still null.


if it is helped this is my next code:

"use server";

import { BrandFormData } from "./types";

export async function createBrand(formData: BrandFormData) {
  try {
    const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
    const response = await fetch(`${baseUrl}/brand`, {
      method: "POST",
      credentials: "include",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(formData),
      cache: "no-store",
    });

    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.message || "Failed to create brand");
    }

    return await response.json();
  } catch (error) {
    console.error("Error creating brand:", error);
    throw error;
  }
}

I am wondering this is because of my next code or not?!

Thank you for ur helping. 🙏 🥰

2 Replies

@Northeast Congo Lion Hi guys i am facing problem i can't handle it. please help me if u can I have project next + nest my next run at http://localhost:3001 and my nest on http://localhost:3010 and when i sending request to my nest from next i can't have cookies i am wondering why?! This is my code in nestjs: export class MyAuthGuard extends BaseSupabaseAuthGuard { constructor(supabaseClient: SupabaseClient) { super(supabaseClient); } protected extractTokenFromRequest(request: Request): string | undefined { const cookies = request.cookies; console.log(cookies); } } and every time i get null value. i read somewhere that i must change my main.ts to this: const app = await NestFactory.create(AppModule); app.use(cookieParser()); app.enableCors({ origin: 'http://localhost:3001', credentials: true, }); await app.listen(3010); and i wanna mention i done it but still null. if it is helped this is my next code: "use server"; import { BrandFormData } from "./types"; export async function createBrand(formData: BrandFormData) { try { const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL; const response = await fetch(`${baseUrl}/brand`, { method: "POST", credentials: "include", headers: { "Content-Type": "application/json", }, body: JSON.stringify(formData), cache: "no-store", }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || "Failed to create brand"); } return await response.json(); } catch (error) { console.error("Error creating brand:", error); throw error; } } I am wondering this is because of my next code or not?! Thank you for ur helping. 🙏 🥰
i always use it for APIs outside for next-js