2

Is it possible to conditionally use app.use in app.js? Express cookie-session can not change the value of maxAge dynamically and I was thinking of doing something of this kind, but I'm getting some errors:

app.use(function(req,res,next ){
  if(typeof req.session == 'undefined' || req.session.staySignedIn === 'undefined'){
    //removing cookie at the end of the session
    cookieSession({
      httpOnly: true,
      secure: false,
      secureProxy: true,
      keys: ['key1', 'key2']
    });
  }else{
    //removing cookie after 30 days
    cookieSession({
      maxAge: 30*24*60*60*1000, //30 days
      httpOnly: true,
      secure: false,
      secureProxy: true,
      keys: ['key1', 'key2']
    });
  }
  next();
});

Instead of the normal use of it:

app.use(cookieSession({
  httpOnly: true,
  secure: false,
  secureProxy: true,
  keys: ['key1', 'key2']
}));

Right now I'm getting the following error:

Cannot read property 'user' of undefined

I believe it is referring to this line (although it doesn't say where exactly)

req.session.user;
Alvaro
  • 40,778
  • 30
  • 164
  • 336

2 Answers2

4

A middleware in Express is a function such as function (req, res, next) {}. In your example, cookieSession(options) will return such a function, but in your middleware you don't run that, you disregard the return value of cookieSession - i.e. the middleware you want to run. Then you run next().

What you want to do instead is to execute the actual middleware in your, let's call it, conditional middleware. Something like this:

app.use(function (req, res, next) {
  var options = {
    httpOnly: true,
    secure: false,
    secureProxy: true,
    keys: ['key1', 'key2']
  };

  if(typeof req.session == 'undefined' || req.session.staySignedIn === 'undefined') {
    options.maxAge = 30*24*60*60*1000; // 30 days
  }

  return cookieSession(options)(req, res, next);
});
Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
  • It strikes me that it might not be a good idea to initialize the session middleware on each request. I'm not so sure about this solution, you might need a wholly different approach. It's unclear to me why you cannot set the `maxAge` for the individual cookie? E.g. `req.cookie.maxAge = 12345...` I'm pretty sure it has worked in the past? – Linus Thiel Jan 07 '16 at 12:33
  • Check this conversation: https://github.com/expressjs/cookie-session/issues/21#issuecomment-52973886 It doesn't seem possible. – Alvaro Jan 07 '16 at 12:46
  • Related issue: http://stackoverflow.com/questions/34656168/stay-signed-in-option-with-cookie-session-in-express – Alvaro Jan 07 '16 at 13:31
0

You can use this plugin Express Conditional Tree Middleware.

It allows you to combine multiple and async middleware. Check it out! You can create two classes (one for your fist case and one for your second case), write your code respectively inside the applyMiddleware functions and then import those classes inside your main javascript file and combine them using an orChainer. See the docs for more info!

DevForce
  • 21
  • 3