Skip to content

Commit

Permalink
Support setting cookie domain on a per-request basis.
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulgi committed Apr 23, 2024
1 parent 98630aa commit 44505f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ContextSession {
debug('decode %j error: %s', cookie, err);
if (!(err instanceof SyntaxError)) {
// clean this cookie to ensure next request won't throw again
ctx.cookies.set(opts.key, '', opts);
ctx.cookies.set(opts.key, '', util.modifyOptsForRequest(ctx, opts));
// ctx.onerror will unset all headers, and set those specified in err
err.headers = {
'set-cookie': ctx.response.get('set-cookie'),
Expand Down Expand Up @@ -287,7 +287,7 @@ class ContextSession {
const externalKey = this.externalKey;

if (externalKey) await this.store.destroy(externalKey, { ctx });
ctx.cookies.set(key, '', opts);
ctx.cookies.set(key, '', util.modifyOptsForRequest(ctx, opts));
}

/**
Expand Down Expand Up @@ -328,7 +328,7 @@ class ContextSession {
if (opts.externalKey) {
opts.externalKey.set(this.ctx, externalKey);
} else {
this.ctx.cookies.set(key, externalKey, opts);
this.ctx.cookies.set(key, externalKey, util.modifyOptsForRequest(this.ctx, opts));
}
return;
}
Expand All @@ -338,7 +338,7 @@ class ContextSession {
json = opts.encode(json);
debug('save %s', json);

this.ctx.cookies.set(key, json, opts);
this.ctx.cookies.set(key, json, util.modifyOptsForRequest(this.ctx, opts));
}
}

Expand Down
8 changes: 8 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,13 @@ module.exports = {
return crc(JSON.stringify(sess));
},

modifyOptsForRequest(ctx, opts) {
const optsCopy = Object.assign({}, opts);
if (typeof opts.domain === "function") {

Check failure on line 40 in lib/util.js

View workflow job for this annotation

GitHub Actions / build (8)

Strings must use singlequote

Check failure on line 40 in lib/util.js

View workflow job for this annotation

GitHub Actions / build (10)

Strings must use singlequote

Check failure on line 40 in lib/util.js

View workflow job for this annotation

GitHub Actions / build (12)

Strings must use singlequote

Check failure on line 40 in lib/util.js

View workflow job for this annotation

GitHub Actions / build (14)

Strings must use singlequote

Check failure on line 40 in lib/util.js

View workflow job for this annotation

GitHub Actions / build (16)

Strings must use singlequote
optsCopy.domain = opts.domain(ctx, opts);
}
return optsCopy;
},

CookieDateEpoch: 'Thu, 01 Jan 1970 00:00:00 GMT',
};

0 comments on commit 44505f9

Please sign in to comment.