Next.js Discord

Discord Forum

Delete cookies for main and sub-domain nextjs v14

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Avatar
Masai LionOP
Hi there,

I need to delete a cookie for subdomains of a base domain.

For example, if my site is www.mysite.com, I have a cookie that is available for abc.mysite.com.

From the backend, two cookies are set: JSESSIONID and access_token. However, when I try to delete the access_token, it doesn't seem to work.

Here’s the information about the cookies:

Key Domain
JSESSIONID mysite.com
access_token app.mysite.com

I've tried the following code to delete the cookie, but it doesn't seem to be effective:
document.cookie = 'cookie_name=; domain=.mysite.com; expires=Thu, 01 Jan 1970 00:00:01 GMT;';


Additionally, I created a server action for logging out the user:
export async function logOutUser() {
  try {
    const response = await get(`${liveHost}/logout`);
    revalidatePath("/");
    
    cookies().delete("JSESSIONID");
    cookies().delete("access_token");
    
    return NextResponse.redirect("/", {
      headers: {
        "Clear-Site-Data": `"*"`,
        "Cache-Control": "must-revalidate",
      },
    });
  } catch (error) {
    console.error("Error logging out user", error);
    throw error;
  }
}


Any suggestions on how to properly delete the access_token cookie for the subdomain?

Thank you!

2 Replies

Avatar
Cape lion
Hi does the deletion in the server action not work? If not, why?
Avatar
Masai LionOP
Hey @Silky Terrier
It is only deleting the JSESSIONID key from the cookies, but not able to delete the access_token.