Skip to content

feat: add support to dynamic cookie options#1027

Merged
UlisesGascon merged 5 commits into
expressjs:masterfrom
lincond:master
Jan 19, 2026
Merged

feat: add support to dynamic cookie options#1027
UlisesGascon merged 5 commits into
expressjs:masterfrom
lincond:master

Conversation

@lincond

@lincond lincond commented Feb 26, 2025

Copy link
Copy Markdown
Contributor

This patch introduces support for dynamic cookie generation in express-session. Instead of only accepting a static object for the options.cookie parameter, the patch allows developers to supply a function that returns a cookie options object. This enables dynamic generation of cookie settings on a per-request basis.

Changes Made:

- JSDoc Update:
The type for options.cookie has been updated from Object to Object|Function to reflect the new functionality

- Dynamic Cookie Options Resolution:
In the session middleware, when generating a new session, the code now checks if cookieOptions is a function. If it is, it calls the function with the current request (req) as an argument to retrieve the cookie options. Otherwise, it uses the static options as before.

req.session.cookie = new Cookie(
  typeof cookieOptions === 'function' ? cookieOptions(req) : cookieOptions
);

- Path Mismatch Check:
The cookie path validation now also resolves the cookie options dynamically to ensure that the generated cookie options are used in the check.

var resolvedCookieOptions = typeof cookieOptions === 'function' ? cookieOptions(req) : cookieOptions;
if (originalPath.indexOf(resolvedCookieOptions.path || '/') !== 0) {
  // Handle pathname mismatch
  debug('pathname mismatch');
  next();
  return;
}

Motivation and Use Case:

This change was made to address scenarios where the cookie configuration (e.g., the cookie path) needs to be determined dynamically based on the incoming request. For example, in multi-tenant applications where different URL paths should result in different cookie configurations, this enhancement allows the session middleware to generate the correct cookie settings on the fly.

Testing:

To test this change, you can set the options.cookie to a function that returns a cookie options object based on the request. Verify that:

  • When a function is provided, it correctly generates the cookie settings for each request.
  • When a static object is provided, the behavior remains unchanged.

Feel free to provide any feedback or suggestions for further improvements.

@bjohansebas

Copy link
Copy Markdown
Member

I like the idea, although for it to be accepted, you need to add tests to verify the behaviors.

@lincond

lincond commented Feb 26, 2025

Copy link
Copy Markdown
Contributor Author

I like the idea, although for it to be accepted, you need to add tests to verify the behaviors.

I'll work on it!

@lincond

lincond commented Feb 26, 2025

Copy link
Copy Markdown
Contributor Author

@bjohansebas Added tests to validate dynamic cookie generation via a function for the cookie option.

@raphendyr

Copy link
Copy Markdown

As an observation, this is targeting v1/master and there is now some work on v2 with breaking changes. While this is not such a thing, it still might be good to know.

I started to prototype idea to put the whole cookie stuff behind class/api/something, so that people could swap it over or reimplement for custom needs. The prototype is here: raphendyr@85c9bd9

When working on that, I keep this PR's need in mind too (where this merged or not).

@lincond

lincond commented Mar 17, 2025

Copy link
Copy Markdown
Contributor Author

@raphendyr good observation. This feature is already in use in one of the projects I work on and is working well.

As I didn't know how long until v2 then I submitted it straight to v1 as it would be great to install the library with this functionality straight from npm as soon as possible.

Your idea of separating the logic from the cookie sounds interesting. If you need any help porting this functionality to v2 I'm happy to help.

@lincond

lincond commented Mar 17, 2025

Copy link
Copy Markdown
Contributor Author

@bjohansebas added an example of how to use the callback as cookie options to README.md in addition to the tests. If you think you need anything else, let me know

@lincond lincond changed the title feat: add support to dynamic cookie generation feat: add support to dynamic cookie options generation Mar 17, 2025
@bjohansebas

Copy link
Copy Markdown
Member

I guess it could go into version 1 and be backported to the v2 branch if this gets merged, What do you think, @UlisesGascon?

Comment thread README.md
@lincond

lincond commented Mar 22, 2025

Copy link
Copy Markdown
Contributor Author

Closes #988

@lincond lincond changed the title feat: add support to dynamic cookie options generation feat: add support to dynamic cookie options Apr 4, 2025
@Goryounov

Copy link
Copy Markdown

Hello! Could you please let me know when the merge is planned? It's a very useful feature that, by the way, will replace this pull.

@lincond

lincond commented Apr 16, 2025

Copy link
Copy Markdown
Contributor Author

Hello! Could you please let me know when the merge is planned? It's a very useful feature that, by the way, will replace this pull.

Hello @Goryounov

I'm waiting for @UlisesGascon review so that the PR can be approved and merged. I'm glad to know that this PR will also be useful to you

@lincond

lincond commented Jul 23, 2025

Copy link
Copy Markdown
Contributor Author

Hi all,

I've just rebased my branch with the latest upstream/master to incorporate recent commits.

@UlisesGascon, could you please review this PR when you have a moment?

@bjohansebas, would you mind assigning this to Ulises?

Thanks!

@bjohansebas
bjohansebas requested a review from efekrskl January 17, 2026 22:22

@UlisesGascon UlisesGascon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Great work @lincond for a first time contribution to this library!

@UlisesGascon UlisesGascon self-assigned this Jan 19, 2026
@UlisesGascon
UlisesGascon merged commit 2673736 into expressjs:master Jan 19, 2026
29 checks passed
This was referenced Jan 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants