How to update session data if already session created express-session

93 views Asked by At

I am creating session using express-session module. Session is creating and saving in session collection succesfully and working fine. Below is express-session configuration.

var session = expressSession({
    saveUninitialized : false,
    resave : true,
    secret :  'asfndu123nisdf2u34',
    cookie: {
        path: "/",
        maxAge: appConfig.maxAge
    },
    name : appConfig.sessionKey,
    store : appConfig.mongoStore
});

login code snippt:

login: function(req, res, next){
             userModel.findOne({email:req.body.email}, function(err, user){
             if(user.password !== req.body.password){
                return res.json({"Wrong Credentials"});
             }else{
                req.session.user = user;
                req.session.save();
             }
             })
        }

If I am loging in a browser then its creating a document in session collection. and if with same credential again login in another browser then also its creating a new document in session collection.

Is it right approach to create a new document(row) in session collection everytime? If not then please let me know how I can check and update the session if already loggedin.

Thanks.

0

There are 0 answers