Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Cookie setup
  2. app.use(session({
  3.     cookieName: 'session',
  4.     secret: 'JMAeDgvzU6yQOQlSeItQ',
  5.     duration: 24 * 60 * 60 * 1000,
  6.     activeDuration: 12 * 60 * 60 * 1000,
  7.     httpOnly: true,
  8.     secure: true,
  9.     ephemeral: true
  10. }));
  11.  
  12. // Cookie session handling middleware
  13. app.use(function (req, res, next) {
  14.     if (req.session && req.session.user) {
  15.         User.findOne({username: req.session.user.username}).lean().exec(function (err, user) {
  16.             if (user) {
  17.                 var modifiedUser = {userId: user._id, username: user.username};//no password in the cookie
  18.                 req.user = modifiedUser;
  19.                 req.session.user = modifiedUser;  //refresh the session value
  20.                 res.locals.user = modifiedUser;
  21.             }
  22.             // finishing processing the middleware and run the route
  23.             next();
  24.         });
  25.     } else {
  26.         next();
  27.     }
  28. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement