Skip to content

Loading…

Added domain option #20

Merged
merged 1 commit into from

2 participants

@richmarr

Allows client code to optionally add a domain parameter to control the scope of a session within subdomains. Includes additions to tests and readme.

@caolan caolan merged commit 12f63d4 into caolan:master
@swang swang referenced this pull request
Closed

Can we get a new npm package? #28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 2 deletions.
  1. +1 −0 README.md
  2. +1 −0 lib/cookie-sessions.js
  3. +3 −2 test/test-cookie-sessions.js
View
1 README.md
@@ -28,6 +28,7 @@ number of options:
* session_key -- The cookie key name to store the session data in
(default: _node)
* path -- The path to use for the cookie (default: '/')
+ * domain -- (optional) Define a specific domain/subdomain scope for the cookie
## Why store session data in cookies?
View
1 lib/cookie-sessions.js
@@ -57,6 +57,7 @@ var exports = module.exports = function(settings){
+ '; expires=' + exports.expires(s.timeout)
+ '; path=' + s.path + '; HttpOnly';
}
+ if ( s.domain ) cookiestr +='; Domain=' + s.domain;
if (cookiestr !== undefined) {
if(Array.isArray(headers)) headers.push(['Set-Cookie', cookiestr]);
View
5 test/test-cookie-sessions.js
@@ -348,7 +348,8 @@ exports['writeHead'] = function(test){
var s = {
session_key:'_node',
secret: 'secret',
- timeout: 86400
+ timeout: 86400,
+ domain:'.domain.com'
};
var req = {headers: {cookie: "_node="}, url: '/'};
var res = {
@@ -357,7 +358,7 @@ exports['writeHead'] = function(test){
headers['Set-Cookie'],
'_node=serialized_session; ' +
'expires=expiry_date; ' +
- 'path=/; HttpOnly'
+ 'path=/; HttpOnly; Domain=.domain.com'
);
test.equals(headers['original'], 'header');
}
Something went wrong with that request. Please try again.