angularjscookiesangular-cookies

How to keep ngCookies after closing browser?


I want to keep my ngCookies after I close the browser. After some research I tried this solution:

var expireDate = new Date();
expireDate.setDate(expireDate.getDate() + 1);
$cookies.put('authenticated', true, {expires: expireDate});

But it sadly only works on F5(refresh). How can I keep the cookies after closing my browser?


Solution

  • Use $cookiesProvider to change the default behavior of the $cookies service.

    expires - {string|Date} - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT" or a Date object indicating the exact date/time this cookie will expire.

    Reference : https://docs.angularjs.org/api/ngCookies/provider/$cookiesProvider#defaults