Couldn't set cookie using ngx-cookie-service

3.1k Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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);