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
  • 3,122
  • 5
  • 31
  • 66
John
  • 1,111
  • 1
  • 13
  • 29

1 Answers1

3

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);
nipuna777
  • 5,039
  • 2
  • 31
  • 46