Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dist
.DS_Store
bower.json
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ and [System.Uri.UnescapeDataString](http://msdn.microsoft.com/en-us/library/syst

Sets a cookie in the document. If the cookie does not already exist, it will be created. Returns the `Cookies` object.

| Option | Description | Default |
| --------: | ------------------------------------------------------------------------------------------------ | ----------- |
| *path* | A string value of the path of the cookie | `"/"` |
| *domain* | A string value of the domain of the cookie | `undefined` |
| *expires* | A number (of seconds), a date parsable string, or a `Date` object of when the cookie will expire | `undefined` |
| *secure* | A boolean value of whether or not the cookie should only be available over SSL | `false` |
| Option | Description | Default |
| ---------: | -------------------------------------------------------------------------------------------------------------------| ----------- |
| *path* | A string value of the path of the cookie | `"/"` |
| *domain* | A string value of the domain of the cookie | `undefined` |
| *expires* | A number (of seconds), a date parsable string, or a `Date` object of when the cookie will expire | `undefined` |
| *secure* | A boolean value of whether or not the cookie should only be available over SSL | `false` |
| *SameSite* | A string value that allows you to declare if the cookies should be restrcited to first party or same site context | `null` |

A default value for any option may be set in the `Cookies.defaults` object.

Expand Down Expand Up @@ -171,12 +172,13 @@ if (Cookies.enabled) {
#### Cookies.defaults
An object representing default options to be used when setting and expiring cookie values.

| Option | Description | Default |
| --------: | ------------------------------------------------------------------------------------------------ | ----------- |
| *path* | A string value of the path of the cookie | `"/"` |
| *domain* | A string value of the domain of the cookie | `undefined` |
| *expires* | A number (of seconds), a date parsable string, or a `Date` object of when the cookie will expire | `undefined` |
| *secure* | A boolean value of whether or not the cookie should only be available over SSL | `false` |
| Option | Description | Default |
| ---------: | ------------------------------------------------------------------------------------------------------------------| ----------- |
| *path* | A string value of the path of the cookie | `"/"` |
| *domain* | A string value of the domain of the cookie | `undefined` |
| *expires* | A number (of seconds), a date parsable string, or a `Date` object of when the cookie will expire | `undefined` |
| *secure* | A boolean value of whether or not the cookie should only be available over SSL | `false` |
| *SameSite* | A string value that allows you to declare if the cookies should be restrcited to first party or same site context | `null` |

**Example Usage**
```javascript
Expand Down
7 changes: 5 additions & 2 deletions src/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

Cookies.defaults = {
path: '/',
secure: false
secure: false,
sameSite: null
};

Cookies.get = function (key) {
Expand Down Expand Up @@ -59,7 +60,8 @@
path: options && options.path || Cookies.defaults.path,
domain: options && options.domain || Cookies.defaults.domain,
expires: options && options.expires || Cookies.defaults.expires,
secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure
secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure,
sameSite: options && options.sameSite || Cookies.defaults.sameSite
};
};

Expand Down Expand Up @@ -95,6 +97,7 @@
cookieString += options.domain ? ';domain=' + options.domain : '';
cookieString += options.expires ? ';expires=' + options.expires.toUTCString() : '';
cookieString += options.secure ? ';secure' : '';
cookieString += options.sameSite ? ';SameSite=' + options.sameSite : '';

return cookieString;
};
Expand Down
36 changes: 28 additions & 8 deletions tests/spec/cookies-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ describe('UNIT TESTS', function () {
it('has a defined `secure` value of `false`', function () {
expect(Cookies.defaults.secure).toBe(false);
});

it('has a defined `sameSite` value of `null`', function () {
expect(Cookies.defaults.sameSite).toBe(null);
});
});

describe('Cookies.get(key)', function () {
Expand Down Expand Up @@ -139,7 +143,8 @@ describe('UNIT TESTS', function () {
path: '/cookies',
domain: 'www.scotthamper.com',
expires: '01/01/2013 GMT',
secure: false
secure: false,
sameSite: null
};
});

Expand Down Expand Up @@ -186,7 +191,8 @@ describe('UNIT TESTS', function () {
path: Cookies.defaults.path,
domain: Cookies.defaults.domain,
expires: new Date(Cookies.defaults.expires),
secure: Cookies.defaults.secure
secure: Cookies.defaults.secure,
sameSite: Cookies.defaults.sameSite
};

spyOn(Cookies, '_generateCookieString').andCallThrough();
Expand Down Expand Up @@ -230,7 +236,8 @@ describe('UNIT TESTS', function () {
path: '/cookies',
domain: 'www.scotthamper.com',
expires: '01/01/2013',
secure: false
secure: false,
sameSite: null
};
});

Expand All @@ -247,7 +254,8 @@ describe('UNIT TESTS', function () {
path: '/nom',
domain: 'www.github.com',
expires: '02/02/2013',
secure: true
secure: true,
sameSite: 'None'
};

expect(Cookies._getExtendedOptions(options)).toEqual(options);
Expand All @@ -260,7 +268,8 @@ describe('UNIT TESTS', function () {
path: options.path,
domain: Cookies.defaults.domain,
expires: Cookies.defaults.expires,
secure: Cookies.defaults.secure
secure: Cookies.defaults.secure,
sameSite: Cookies.defaults.sameSite
});
});

Expand All @@ -271,7 +280,8 @@ describe('UNIT TESTS', function () {
path: Cookies.defaults.path,
domain: options.domain,
expires: Cookies.defaults.expires,
secure: Cookies.defaults.secure
secure: Cookies.defaults.secure,
sameSite: Cookies.defaults.sameSite
});
});

Expand All @@ -282,7 +292,8 @@ describe('UNIT TESTS', function () {
path: Cookies.defaults.path,
domain: Cookies.defaults.domain,
expires: options.expires,
secure: Cookies.defaults.secure
secure: Cookies.defaults.secure,
sameSite: Cookies.defaults.sameSite
});
});

Expand All @@ -293,7 +304,8 @@ describe('UNIT TESTS', function () {
path: Cookies.defaults.path,
domain: Cookies.defaults.domain,
expires: Cookies.defaults.expires,
secure: options.secure
secure: options.secure,
sameSite: Cookies.defaults.sameSite
});
});

Expand All @@ -311,6 +323,7 @@ describe('UNIT TESTS', function () {
expect(options.domain).toBeUndefined();
expect(options.expires).toBeUndefined();
expect(options.secure).toBeUndefined();
expect(options.sameSite).toBeUndefined();
});
});

Expand Down Expand Up @@ -434,6 +447,13 @@ describe('UNIT TESTS', function () {
var options = { secure: true };
expect(Cookies._generateCookieString(key, value, options)).toEqual('key=value;secure');
});

it('includes the SameSite flag when `options.sameSite` is defined', function () {
var options = { sameSite: 'None' };
var expected = 'key=value;SameSite=None';

expect(Cookies._generateCookieString(key, value, options)).toEqual(expected);
});
});

describe('Cookies._getCacheFromString(documentCookie)', function () {
Expand Down