expressjs / session

Simple session middleware for Express
MIT License
6.21k stars 972 forks source link

Not possible to set cookie encode option #883

Open thomasmarr opened 2 years ago

thomasmarr commented 2 years ago

One of the options which can be set on cookie.serialize is encode see here

In express-session it is not possible to set this option:

I believe express-session should expose this, as the default encodeUriComponent is not always the desired behaviour. I would be willing to try and put a PR together for this if it was likely to be merged promptly.

FlinthDevs commented 9 months ago

I agree, in my context, cookies are urlEncoded once more by a mechanism I have no control on and I had to patch the module to allow encode to be modified same as other ones.

diff --git a/node_modules/express-session/session/cookie.js b/node_modules/express-session/session/cookie.js
index a8b4e57..5fa4553 100644
--- a/node_modules/express-session/session/cookie.js
+++ b/node_modules/express-session/session/cookie.js
@@ -73,6 +73,28 @@ Cookie.prototype = {
     return this._expires;
   },

+  /**
+  * Set encode `callback`.
+  *
+  * @param {CallableFunction} callback
+  * @api public
+  */
+
+ set encode(callback) {
+   this.callback = callback;
+ },
+
+ /**
+  * Get encode `callback`.
+  *
+  * @return {CallableFunction} callback
+  * @api public
+  */
+
+ get encode() {
+   return this.callback;
+ },
+
   /**
    * Set expires via max-age in `ms`.
    *
@@ -123,6 +145,7 @@ Cookie.prototype = {
       , domain: this.domain
       , path: this.path
       , sameSite: this.sameSite
+      , encode: this.callback
     }
   },