I want to implement cart in my drf. and I am using session, But when I send a cart modification request like 'add' it creates a new session row everytime and it isnt what i want
it works when I test with Bruno api testing, the cart updates and increament without any duplication,the session is maintained, but when I am consuming the api from my next.js, that is when the whole duplication and all happens
#new cookies created on every request
1 messages · Page 1 of 1 (latest)
Hi @hard iris. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
It seems like you're experiencing an issue where a new session is created every time a cart modification request is made from your Next.js application. This could be due to how cookies are being handled in your Next.js application.
In Next.js, cookies can be set and managed in various ways. For instance, you can use the cookies module from next/headers to handle cookies in server-side logic or middleware. Here's an example of how to set a cookie on the server:
'use server'
import { cookies } from 'next/headers'
export async function handleLogin(sessionData) {
const encryptedSessionData = encrypt(sessionData) // Encrypt your session data
cookies().set('session', encryptedSessionData, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
maxAge: 60 * 60 * 24 * 7, // One week
path: '/',
})
// Redirect or handle the response after setting the cookie
}
And here's how you can access the session data stored in the cookie in a server component:
import { cookies } from 'next/headers'
export async function getSessionData(req) {
const encryptedSessionData = cookies().get('session')?.value
return encryptedSessionData ? JSON.parse(decrypt(encryptedSessionData)) : null
}
(Source: Next.js Docs (https://nextjs.org/docs/app/building-your-application/authentication#cookie-based-sessions))
However, if you're experiencing issues with multiple cookies being set, it could be due to how your application is handling cookies with the same name. According to an open issue on the Next.js GitHub repository, Next.js shouldn't combine or return multiple cookies with the same value. Instead, they should be overwritten before sending it back in the response (Source (https://github.com/vercel/next.js/issues/64698)).
If you're using a central session store like Vercel KV, you might want to ensure that your session IDs are being handled correctly. Here's an example of how to handle session IDs in Next.js with Vercel KV:
type SessionId = string;
export function getSessionId(): SessionId | undefined {
const cookieStore = cookies();
return cookieStore.get("session-id")?.value;
}
function setSessionId(sessionId: SessionId): void {
const cookieStore = cookies();
cookieStore.set("session-id", sessionId);
}
export function getSessionIdAndCreateIfMissing() {
const sessionId = getSessionId();
if (!sessionId) {
const newSessionId = crypto.randomUUID();
setSessionId(newSessionId);
return newSessionId;
}
return sessionId;
}
(Source: Vercel Guides (https://vercel.com/guides/session-store-nextjs-redis-vercel-kv#storing-sessions-with-redis))