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

feat(ngCookies): Additional parameters for $cookies service (domain, expires, secure) #6335

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ function Browser(window, document, $log, $sniffer) {
//////////////////////////////////////////////////////////////
var lastCookies = {};
var lastCookieString = '';
var cookiePath = self.baseHref();
var cookieOptions = self.cookieOptions = {
path: self.baseHref()
};

/**
* @name $browser#cookies
Expand All @@ -282,15 +284,28 @@ function Browser(window, document, $log, $sniffer) {
self.cookies = function(name, value) {
/* global escape: false, unescape: false */
var cookieLength, cookieArray, cookie, i, index;
var expireDate = new Date('Thu, 01 Jan 1970 00:00:00 GMT');

function makeCookie(name, value, opt) {
if (opt.expires && !(opt.expires instanceof Date)) {
opt.expires = new Date(new Date().getTime() + opt.expires * 1000);
}

return [
escape(name) + '=' + escape(value),
opt.expires ? '; expires=' + opt.expires.toUTCString() : '',
opt.path ? '; path=' + opt.path : '',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave here the cookiePath as a fallback, so

  • not specifying the path has the same effect like before patch
  • this is IMHO good default value; it's easier to specify '' as a option then to pass the basePath every time we set cookie.
'; path=' + typeof opt.path === 'string' ? opt.path : cookiePath,

opt.domain ? '; domain=' + opt.domain : '',
opt.secure ? '; secure' : ''
].join('');
}

if (name) {
if (value === undefined) {
rawDocument.cookie = escape(name) + "=;path=" + cookiePath +
";expires=Thu, 01 Jan 1970 00:00:00 GMT";
rawDocument.cookie = makeCookie(name, '', { expires: expireDate });
} else {
if (isString(value)) {
cookieLength = (rawDocument.cookie = escape(name) + '=' + escape(value) +
';path=' + cookiePath).length + 1;
cookieLength = (rawDocument.cookie = makeCookie(name, value, cookieOptions)).length;

// per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
// - 300 cookies
Expand Down