Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 53c6636

Browse files
shahatapetebacondarwin
authored andcommitted
feat($cookiesProvider): provide path, domain, expires and secure options
This change provides properties on `$cookiesProvider` so that you can set the application level default options for cookies that are set using the `$cookies` service
1 parent 92c366d commit 53c6636

File tree

2 files changed

+125
-40
lines changed

2 files changed

+125
-40
lines changed

src/ngCookies/cookies.js

+69-40
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,66 @@
1919

2020
angular.module('ngCookies', ['ng']).
2121
/**
22-
* @ngdoc service
23-
* @name $cookies
24-
*
22+
* @ngdoc provider
23+
* @name $cookiesProvider
2524
* @description
26-
* Provides read/write access to browser's cookies.
27-
*
28-
* BREAKING CHANGE: `$cookies` no longer exposes properties that represent the
29-
* current browser cookie values. Now you must use the get/put/remove/etc. methods
30-
* as described below.
31-
*
32-
* Requires the {@link ngCookies `ngCookies`} module to be installed.
33-
*
34-
* @example
35-
*
36-
* ```js
37-
* angular.module('cookiesExample', ['ngCookies'])
38-
* .controller('ExampleController', ['$cookies', function($cookies) {
39-
* // Retrieving a cookie
40-
* var favoriteCookie = $cookies.get('myFavorite');
41-
* // Setting a cookie
42-
* $cookies.put('myFavorite', 'oatmeal');
43-
* }]);
44-
* ```
45-
*/
46-
factory('$cookies', ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
25+
* Use `$cookiesProvider` to change the default behavior of the {@link ngCookies.$cookies $cookies} service.
26+
* */
27+
provider('$cookies', [function $CookiesProvider() {
28+
/**
29+
* @ngdoc property
30+
* @name $cookiesProvider#defaults
31+
* @description
32+
*
33+
* Object containing default options to pass when setting cookies.
34+
*
35+
* The object may have following properties:
36+
*
37+
* - **path** - `{string}` - The cookie will be available only for this path and its
38+
* sub-paths. By default, this would be the URL that appears in your base tag.
39+
* - **domain** - `{string}` - The cookie will be available only for this domain and
40+
* its sub-domains. For obvious security reasons the user agent will not accept the
41+
* cookie if the current domain is not a sub domain or equals to the requested domain.
42+
* - **expires** - `{string|Date}` - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT"
43+
* or a Date object indicating the exact date/time this cookie will expire.
44+
* - **secure** - `{boolean}` - The cookie will be available only in secured connection.
45+
*
46+
* Note: by default the address that appears in your <base> tag will be used as path.
47+
* This is import so that cookies will be visible for all routes in case html5mode is enabled
48+
*
49+
**/
50+
var defaults = this.defaults = {};
51+
52+
function calcOptions(options) {
53+
return options ? angular.extend({}, defaults, options) : defaults;
54+
}
55+
56+
/**
57+
* @ngdoc service
58+
* @name $cookies
59+
*
60+
* @description
61+
* Provides read/write access to browser's cookies.
62+
*
63+
* BREAKING CHANGE: `$cookies` no longer exposes properties that represent the
64+
* current browser cookie values. Now you must use the get/put/remove/etc. methods
65+
* as described below.
66+
*
67+
* Requires the {@link ngCookies `ngCookies`} module to be installed.
68+
*
69+
* @example
70+
*
71+
* ```js
72+
* angular.module('cookiesExample', ['ngCookies'])
73+
* .controller('ExampleController', ['$cookies', function($cookies) {
74+
* // Retrieving a cookie
75+
* var favoriteCookie = $cookies.get('myFavorite');
76+
* // Setting a cookie
77+
* $cookies.put('myFavorite', 'oatmeal');
78+
* }]);
79+
* ```
80+
*/
81+
this.$get = ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
4782
return {
4883
/**
4984
* @ngdoc method
@@ -70,7 +105,7 @@ angular.module('ngCookies', ['ng']).
70105
* @returns {Object} Deserialized cookie value.
71106
*/
72107
getObject: function(key) {
73-
var value = $$cookieReader()[key];
108+
var value = this.get(key);
74109
return value ? angular.fromJson(value) : value;
75110
},
76111

@@ -96,20 +131,11 @@ angular.module('ngCookies', ['ng']).
96131
*
97132
* @param {string} key Id for the `value`.
98133
* @param {string} value Raw value to be stored.
99-
* @param {Object=} options Object with options that need to be stored for the cookie.
100-
* The object may have following properties:
101-
*
102-
* - **path** - `{string}` - The cookie will be available only for this path and its
103-
* sub-paths. By default, this would be the URL that appears in your base tag.
104-
* - **domain** - `{string}` - The cookie will be available only for this domain and
105-
* its sub-domains. For obvious security reasons the user agent will not accept the
106-
* cookie if the current domain is not a sub domain or equals to the requested domain.
107-
* - **expires** - `{string|Date}` - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT"
108-
* or a Date object indicating the exact date/time this cookie will expire.
109-
* - **secure** - `{boolean}` - The cookie will be available only in secured connection.
134+
* @param {Object=} options Options object.
135+
* See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
110136
*/
111137
put: function(key, value, options) {
112-
$$cookieWriter(key, value, options);
138+
$$cookieWriter(key, value, calcOptions(options));
113139
},
114140

115141
/**
@@ -122,9 +148,10 @@ angular.module('ngCookies', ['ng']).
122148
* @param {string} key Id for the `value`.
123149
* @param {Object} value Value to be stored.
124150
* @param {Object=} options Options object.
151+
* See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
125152
*/
126153
putObject: function(key, value, options) {
127-
$$cookieWriter(key, angular.toJson(value), options);
154+
this.put(key, angular.toJson(value), options);
128155
},
129156

130157
/**
@@ -136,9 +163,11 @@ angular.module('ngCookies', ['ng']).
136163
*
137164
* @param {string} key Id of the key-value pair to delete.
138165
* @param {Object=} options Options object.
166+
* See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
139167
*/
140168
remove: function(key, options) {
141-
$$cookieWriter(key, undefined, options);
169+
$$cookieWriter(key, undefined, calcOptions(options));
142170
}
143171
};
144-
}]);
172+
}];
173+
}]);

test/ngCookies/cookiesSpec.js

+56
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,60 @@ describe('$cookies', function() {
8383
$cookies.remove('name', {path: '/a/b'});
8484
expect($$cookieWriter).toHaveBeenCalledWith('name', undefined, {path: '/a/b'});
8585
}));
86+
87+
88+
it('should pass default options on put', function() {
89+
module(function($cookiesProvider) {
90+
$cookiesProvider.defaults.secure = true;
91+
});
92+
inject(function($cookies, $$cookieWriter) {
93+
$cookies.put('name', 'value', {path: '/a/b'});
94+
expect($$cookieWriter).toHaveBeenCalledWith('name', 'value', {path: '/a/b', secure: true});
95+
});
96+
});
97+
98+
99+
it('should pass default options on putObject', function() {
100+
module(function($cookiesProvider) {
101+
$cookiesProvider.defaults.secure = true;
102+
});
103+
inject(function($cookies, $$cookieWriter) {
104+
$cookies.putObject('name', 'value', {path: '/a/b'});
105+
expect($$cookieWriter).toHaveBeenCalledWith('name', '"value"', {path: '/a/b', secure: true});
106+
});
107+
});
108+
109+
110+
it('should pass default options on remove', function() {
111+
module(function($cookiesProvider) {
112+
$cookiesProvider.defaults.secure = true;
113+
});
114+
inject(function($cookies, $$cookieWriter) {
115+
$cookies.remove('name', {path: '/a/b'});
116+
expect($$cookieWriter).toHaveBeenCalledWith('name', undefined, {path: '/a/b', secure: true});
117+
});
118+
});
119+
120+
121+
it('should let passed options override default options', function() {
122+
module(function($cookiesProvider) {
123+
$cookiesProvider.defaults.secure = true;
124+
});
125+
inject(function($cookies, $$cookieWriter) {
126+
$cookies.put('name', 'value', {secure: false});
127+
expect($$cookieWriter).toHaveBeenCalledWith('name', 'value', {secure: false});
128+
});
129+
});
130+
131+
132+
it('should pass default options if no options are passed', function() {
133+
module(function($cookiesProvider) {
134+
$cookiesProvider.defaults.secure = true;
135+
});
136+
inject(function($cookies, $$cookieWriter) {
137+
$cookies.put('name', 'value');
138+
expect($$cookieWriter).toHaveBeenCalledWith('name', 'value', {secure: true});
139+
});
140+
});
141+
86142
});

0 commit comments

Comments
 (0)