Skip to content

res.cookie does not allow setting Max-Age only, without Expires (One should be able to set maxAge alone, with expires: 0) #5150

Open
@nbkhope

Description

@nbkhope

I want to set a cookie with Max-Age only, without having Expires. But the following lines 875-876 keep adding the unwanted Expires:

express/lib/response.js

Lines 871 to 878 in 0debedf

if (opts.maxAge != null) {
var maxAge = opts.maxAge - 0
if (!isNaN(maxAge)) {
opts.expires = new Date(Date.now() + maxAge)
opts.maxAge = Math.floor(maxAge / 1000)
}
}

Setting expires: 0 does no good to override that behavior.

I think one should be allowed to only set Max-Age without automatically having Expires also set.

res.cookie('MyCookie', 'TheValue', {
  expires: 0, // I don't want any Expires in the resulting Set-Cookie statement
  maxAge: 60000,
});

Actual result:

MyCookie=TheValue; Max-Age=60; Path=/; Expires=Sun, 26 Mar 2023 06:00:31 GMT

Desired result:

MyCookie=TheValue; Max-Age=60; Path=/; 

Workaround

The workaround for this is our having to manually write the Set-Cookie statement, possibly mimicking the same logic in the express code.

Possible solution

if (opts.expires !== 0) {
  opts.expires = new Date(Date.now() + maxAge)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions