Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set domain for angularJS $cookies

It seems $cookies only allows to set the Key/Value pair.

But i need to set the domain parameter as well. I need to set the cookie from one subdomain of website and use it from all subdomains of website.

I am using the angularJS and Twitter Bootstrap.

like image 556
Bhalchandra K Avatar asked Dec 09 '13 18:12

Bhalchandra K


2 Answers

Currently this is not implemented in angular (1.2.7). You may have a look at angular.js line 4348 - the relevant code a little bit reduced:

if (value === undefined) {
   // Delete the cookie
   rawDocument.cookie = escape(name) + 
          "=;path=" + cookiePath + ";expires=Thu, 01 Jan 1970 00:00:00 GMT";
} else {
   // set or update the cookie value
   rawDocument.cookie = escape(name) + '=' + escape(value) + ';path=' + cookiePath);
}

as you can see the ;domain=XYZ is not included. You have to write your own cookiestore that implements this feature - but this should be very easy.

like image 125
michael Avatar answered Nov 09 '22 19:11

michael


I've created module for AngularJS to solve this issue:

https://github.com/stevermeister/ng-biscuit

now you can simply set options:

cookieStore.put('x', 5, {domain: 'youdomain.com'})
like image 4
Stepan Suvorov Avatar answered Nov 09 '22 18:11

Stepan Suvorov