Next.js Discord

Discord Forum

cookies

Unanswered
Mini Lop posted this in #help-forum
Open in Discord
Mini LopOP
I have cookies but when I fetch I get null. I have and the cookies is there. Please help me

35 Replies

@Mini Lop I have cookies but when I fetch I get null. I have and the cookies is there. Please help me
if your cookie is httponly you can only fetch it on server and not on client
Mini LopOP
Can you help me with example
Ive tried, want to retrieve at api route
@Mini Lop Ive tried, want to retrieve at api route
you can just use cookies function from nextjs to get cookies
@@ts-ignore you can just use `cookies` function from nextjs to get cookies
Mini LopOP
Please help with examples
Still not working
I got null
can you show me your code?
Mini LopOP
Ok
Mini LopOP
i set in cookies in middleware and here is the code export default clerkMiddleware(async (auth, req) => {

if (isProtectedRoute(req)) auth().protect();

// managing new user with referral code
const response = NextResponse.next();

const referral = req.nextUrl.searchParams.get('ref');
console.log("Referral code:", referral);

if (req.nextUrl.pathname.startsWith('/sign-up')) {
// Check if referral code cookie already exists
const { referralCode } = await getCookiesStore();

console.log(referralCode, "get cookie in middlewaer")
if (!referralCode) {
// Setting referral code from search params to cookies
console.log("Setting referral code cookie");
response.cookies.set({
name: "referral_code",
value: referral || "", // Set referral code if present, otherwise empty string
maxAge: 86400,
path: '/'
});
}
}

return response;
});
but i want to use it in api/webhooks/clerk/route.ts so i called this function there to retrieve the cookies with this function "use server"
import { cookies } from "next/headers";

export async function getCookiesStore() {
const cookieStore = cookies();
const referralCodeCookie = await cookieStore.get("referral_code");

// Check if the referral_code cookie exists and has a non-null value
const referralCode = referralCodeCookie && referralCodeCookie.value !== null
? referralCodeCookie.value
: null;

return {
referralCode: referralCode,
// You can add other cookies to return here if needed
};
}
but i got null
Thats all please
@Mini Lop Thats all please
how are you calling this route?
like using your code or is it the clerk that is calling it?
Mini LopOP
I using clerk webhooks
Yes
@Mini Lop I using clerk webhooks
clerk webhooks doesn't supply any cookies
it is clerk who is calling your API not your code hence no cookies
Mini LopOP
So I cant use it?
But I added my function to fetch the cookies
it calls cookies() which access the cookies from current request
clerk doesn't supply any cookies
Mini LopOP
Ok
Then I the task cant perform as I was think, have for find different approach
But wish you can help me
I just told you that the thing you are trying to do is not possible
Mini LopOP
Yes I've understood
But what I trying to is, I've to track users prospect but insert the referral user ID to the fettered user data, so that I can track but because I uses clerk I find difficult to do it. Can you suggests other approches
Thank you
idk if there's a way in clerk to pass custom metadata
Mini LopOP
please i dont get
Mini LopOP
ok