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.
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.
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();
};