1

I'm using angular 9 and I need to set a cookie. I'm using ngx-cookie-service 3.0.4 and I'm trying to do that like the following:

this.cookieService.set("cookieName", user.tokenId, date, "/", "localhost:4200");

and when I'm trying to get out this one I get null in the response and in the browser I'm not seeing this cookie. I'm trying to get out the result like the following:

console.log("COOKIE: ", this.cookieService.get("cookieName));

What do I do wrong?

Pavan Jadda
  • 4,306
  • 9
  • 47
  • 79
John
  • 1,375
  • 4
  • 17
  • 40

1 Answers1

4

When setting cookies for the localhost, you can not use 'localhost:4200' as the domain. This is by design and you can read more about it here: Cookies on localhost with explicit domain

In this case, you can pass in null or omit the domain entirely when the application is running on the localhost.

const hostName = isLocal ? null : 'HOST_NAME';
this.cookieService.set("cookieName", user.tokenId, date, "/", hostName);
nipuna-g
  • 6,252
  • 3
  • 31
  • 48