Next.js Discord

Discord Forum

Deleting cookies not working on server

Answered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
When im trying to delete cookies on server, it wont work. Deleting cookies on localhost works ok. Any ideas what may cause this? Heres my code:

"use server";

import { cookies } from "next/headers";
import { redirect } from "next/navigation";

export async function deleteCookie() {
  await cookies().set("jid", "", {
    maxAge: 0,
    path: "/",
    domain: "localhost",
    httpOnly: true,
  });
  await cookies().set("jid", "", {
    maxAge: 0,
    path: "/",
    domain: ".mydomain.com", // (Tried literally everything here)
    httpOnly: true,
    secure: true,
  });
  cookies().delete("jid");
  redirect("/login");
}
Answered by Sloth bear
finally figured this out. Had to change on server side "secure" to true.

      res.cookie("jid", accessToken, {
        httpOnly: true,
        domain: process.env.NODE_ENV ? "localhost" : "mydomain.com",
        expires: new Date(Date.now() + 60 * 60 * 24 * 60 * 1000),
        secure: process.env.NODE_ENV === "production", // Added this line
      });
View full answer

8 Replies

Sloth bearOP
Update: I tried to log all cookies after deleting, and i get following:

[
  {
    name: 'jid',
    value: '',
    path: '/',
    domain: undefined,
    expires: 1970-01-01T00:00:00.000Z
  }
]


So i tried to delete it without providing domain:
  await cookies().set("jid", "", {
    maxAge: 0,
    path: "/",
    httpOnly: true,
    secure: true,
  });


without success. any ideas?
Sloth bearOP
Anyone please? Still having this issue
Toyger
where you use your delete function?
@Toyger where you use your delete function?
Sloth bearOP
i’m using server action, this snippet is has ”use server” on first line and i call it from UserProvider which has my logout function. And this only happens on server, not on localhost
Toyger
can you leave in function only delete, and check network tab for server action request, which cookies it return, and then check application tab in devtools, it have cookies menu as well, but it show current version not request only
@Sloth bear I tried to have only cookies().delete() called without success. Cookie stays on devtools application tab.
Toyger
what about cookies tab state at request on server action itself? not on application tab.
if there it still show it then before request end you probably have some maybe middleware which again set this cookie
@Toyger what about cookies tab state at request on server action itself? not on application tab. if there it still show it then before request end you probably have some maybe middleware which again set this cookie
Sloth bearOP
finally figured this out. Had to change on server side "secure" to true.

      res.cookie("jid", accessToken, {
        httpOnly: true,
        domain: process.env.NODE_ENV ? "localhost" : "mydomain.com",
        expires: new Date(Date.now() + 60 * 60 * 24 * 60 * 1000),
        secure: process.env.NODE_ENV === "production", // Added this line
      });
Answer