Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cookie setup
- app.use(session({
- cookieName: 'session',
- secret: 'JMAeDgvzU6yQOQlSeItQ',
- duration: 24 * 60 * 60 * 1000,
- activeDuration: 12 * 60 * 60 * 1000,
- httpOnly: true,
- secure: true,
- ephemeral: true
- }));
- // Cookie session handling middleware
- app.use(function (req, res, next) {
- if (req.session && req.session.user) {
- User.findOne({username: req.session.user.username}).lean().exec(function (err, user) {
- if (user) {
- var modifiedUser = {userId: user._id, username: user.username};//no password in the cookie
- req.user = modifiedUser;
- req.session.user = modifiedUser; //refresh the session value
- res.locals.user = modifiedUser;
- }
- // finishing processing the middleware and run the route
- next();
- });
- } else {
- next();
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement