0

I am using cookieSession to deal with session matters. No problem except when i restart the browser, my session doesn't persist. I wish sessions remain active every time and never expire.

Here is my setup

app.use(cookieSession({
  name: 'xxxx',
  keys: ['xxxxx', 'xxxx'],
  cookie: { secure: true,
            httpOnly: true,
            domain: 'mydomain.com',

            expires: 9999999999999999999999999999999999999999 
          }
  })
); 

Any suggestion ?

John doe
  • 3,680
  • 7
  • 31
  • 65

1 Answers1

1

Looking at the cookie-session readme, I don't think you need to nest your options in a cookie property. Also, expires takes a Date object; you should pass that value as maxAge instead if you wish to give it the number of milliseconds until the cookie expires. Keep in mind that all cookies expire per the cookie specification, so the best you can do is set an expiration time very far in the future.

Try this:

app.use(cookieSession({
  name: 'xxxx',
  keys: ['xxxxx', 'xxxx'],
  secure: true,
  httpOnly: true,
  domain: 'mydomain.com',
  maxAge: 9999999999999999999999999999999999999999 
  })
); 

EDIT: Some older browsers don't support maxAge, so expires might be better for compatibility. Also, watch out for overflow in maxAge; a negative value may cause the browser to expire the cookie immediately or when the browser closes. Consider using Number.MAX_SAFE_INTEGER.

Doug W
  • 105
  • 6
  • Thanks for your help. However still the same issue. All cookies are removed when i restart the browser. Very strange. – John doe Jan 04 '19 at 15:50
  • Are you sure a cookie is being sent back from the server? – Doug W Jan 04 '19 at 16:11
  • Solved ! thanks. Indeed the problem was related to the `Date`. `9999999999999999999` was an invalid date and i incorrectly set another timestamp to `expires` which expects a `Date` object. After i set `new Date(Date.now() + 86400 * 1000 * 1000)` Now my issue is fixed . Thank you Doug ! – John doe Jan 04 '19 at 17:57