Search code examples
javascriptnode.jssessionexpress

Node + Express Session Expiration?


I have an app in express and I have a login form. I need sessions to last for 1 month do I set maxAge to a month in milliseconds.

I left two computers on and logged in for 24 hours and when I came back both were logged out.

How do I fix this/achieve what I'm trying to do? Thanks.


Solution

  • You can use the expires attribute instead of maxAge, which takes a Date object as value. Also, check session cookie expires on the client after they set. It's always possible that the session was ended by the server (i.e. memcached restarted).

    This works for me:

    app.use(express.session({
      secret: "secret",
      store: new MemoryStore(),
      expires: new Date(Date.now() + (30 * 24 * 3600 * 1000)) 
    }));
    

    but this also works:

    app.use(express.session({
      secret: "secret",
      store: new MemoryStore(),
      maxAge: Date.now() + (30 * 24 * 3600 * 1000)
    }));