October 22, 2024
Chicago 12, Melborne City, USA
javascript

Get cookie by subdomain wise in react js using universal-cookie


I am using universal-cookie in my React JS project to store a token. I am setting cookies for both the domain and subdomain. My domain is testdomain.com and my subdomain is demo.subdomain.com. When I set the cookie for the domain and try to access it from the subdomain, I am getting the domain cookie instead of the subdomain cookie.

Here is the my function to set the cookie

export const setCookies = (key: string, value: string) => {
  const cookies = new Cookies();
  const currentDate = moment();
  const newDate = currentDate.add(2, "days").toDate();
  cookies.set(key, value, {
    path: "/",
    expires: newDate,
    secure: true,
  });
};

In the component, I am accessing the cookie using getCookies function

export const getCookies = (key: string) => {
  const cookies = new Cookies();
  return cookies.get(key);
};

Here I want to specify the cookie by subdomain. In the cookie it is already stored for both i.e. for subdomain and domain.

I have used same key name.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video