Cookies path from server action
Unanswered
Arboreal ant posted this in #help-forum
Arboreal antOP
I'm working with cookies that have different paths in them but it seems I'm unable to access the path value in delete using the cookies from next/header. Any suggestions? I'm trying to do this in a server action
33 Replies
Asian black bear
how to delete?
Arboreal antOP
I'm doing cookieStore.delete(name) but there is no option to add a path. by default the delete seems to add "/" so it won't delete the cookie
Asian black bear
then try to use cookieStore.set(name, value, option)
cookieStore.set(name, '', {maxAge: 0, path: "..."})
Arboreal antOP
I think the main problem I have is I can't do "cookie.path" and get the value. the requestcookie type only has name and value
Asian black bear
cookieStore is not next/headers cookie?
share the full code plz
Arboreal antOP
"use server";
import { cookies } from "next/headers";
export async function removeCartCookies() {
const cookieStore = await cookies();
cookieStore.getAll().forEach((cookie) => {
if (cookie.name === "cartId" || cookie.name === "cartIdentifier") {
cookieStore.delete({ name: cookie.name, path: cookie.path });
console.log(`Deleted cookie: ${cookie.name} with path: ${cookie.path}`);
}
});
}Asian black bear
try cookiStore.set
it will work ig
Arboreal antOP
cookie path is always / tho
I get this in my logs
[1] Deleted cookie: cartId with path: /
[1] Deleted cookie: cartIdentifier with path: /Asian black bear
did you try cookieStore.set?
Arboreal antOP
but you can see that my cookies have this
Asian black bear
😢 plz try cookieStore.set
Arboreal antOP
just checking
same result.. cookies are still there
"use server";
import { cookies } from "next/headers";
export async function removeCartCookies() {
const cookieStore = await cookies();
cookieStore.getAll().forEach((cookie) => {
if (cookie.name === "cartId" || cookie.name === "cartIdentifier") {
cookieStore.set(cookie.name, "", {
expires: new Date(0),
});
console.log(`Deleted cookie: ${cookie.name} with path: ${cookie.path}`);
}
});
}I'm sure this would work if I could access the path value from the cookie, but right now cookie.path is undefined and it defaults to '/'
I can probably use a route handler and access the response but I was trying to do this with a server action
what i'm building is multi tenanted so I need the path to keep cookies seperated
Asian black bear
not related to your problem, but is there any reason to put await to cookies?
Arboreal antOP
I'm running on canary 180. since 171 they made any header functions (including searchParams and params) async.
Asian black bear
ofc you set path when set cookie, or is it set automatically?
what I'm thinking there's no way to get path in next.js
it's not that path value is null but you just can't get it using next.js code
Arboreal antOP
I set the path when I create the cookie.. this identifies the tenant the cookie is connected with
it does seem that next is lacking in this area with their functions. I'll go do this in a route handler that can access the full cookie
Asian black bear
I think cookies.getAll returns ones whose the path is currentPath.
anyhow, good luck with your approach. sorry to not help you. hehe
Arboreal antOP
thanks! get all does return the ones with the other path but you can't accesss it and the delete always uses /.. but thanks for the assistance
I might log an issue on this