Thursday, 16 March 2017

NodeJS - Create a object in the session

I am trying to create an object and store it in the session in order to access it everywhere.

Require:

var session = require('express-session');

In app.js:

app.use(session({
    secret: '1234567890poiuytrewq1234567890-.,',
    resave: false,
    saveUninitialized: false
}));
app.use(function(req, res, next){
   if(!req.session.auth){
       req.session.auth = new AuthService();
   }
   next();
});

If I console.log(req.session.auth) then the object is OK. But if I go to a route the object is not there:

router.post('/login', function(req, res){
    if(req.body.remember){
        //I'll do this later
    }
    var username = req.body.username;
    var cryptPass = md5(req.body.password);
    res.send(req.session.auth);
});

I am new to NodeJS, what I am doing wrong handling sessions?



via Programador Adagal

No comments:

Post a Comment