req.session on node.js

2

I would like to know how the basic operation of the req.session structure of Node.js. Is it native to Express or Express-Session? Please help me understand more about sessions.

    
asked by anonymous 28.08.2015 / 14:43

1 answer

1

It is installed separately using the command "npm install express-session --save"

Then put this in your main file:

app.use(session({
    secret: '2C44-4D44-WppQ38S',
    resave: true,
    saveUninitialized: true
}));

And to create a new session variable, follow the example:

req.session.usuario = usuario;

And a basic middleware to authenticate would be:

module.exports = function(req, res, next) {
  if(!req.session.usuario) {
      return res.redirect('/');
  }
    return next();
};
    
21.09.2015 / 04:36