2

I am new to MEAN stack, presently the mean stack is inserting sessions to mongodb:

app.use(session({
  saveUninitialized: true,
  resave: true,
  //cookie: { maxAge: 600  },
  secret: config.sessionSecret,
  store: newmongoStore({
    db: db.connection.db,
    collection: config.sessionCollection
  })
})); 

But I want to save some custom variables in that session & access them across requests, I did not get how to save it in session. Let's say I want to save mydata in session, I saw some examples & tried like:

req.session.mydata = 'projectdata';
req.session.cookie.mydata = 'projectdata';

Both are not working. Also I want to update maxAge variable on every request to server side, how to do it? Kindly help me.

chridam
  • 100,957
  • 23
  • 236
  • 235
Nandish
  • 125
  • 2
  • 11
  • Can you provide the code you are saving into `req.session`? – Khay Mar 04 '15 at 07:23
  • It's already there in my question. According to below link http://stackoverflow.com/questions/5765777/node-js-how-to-create-and-read-session-with-express i did like: req.session.mydata = 'projectdata'; and req.session.cookie.mydata = 'projectdata'; – Nandish Mar 04 '15 at 09:40
  • Are you using `express-session`? – Khay Mar 04 '15 at 09:48
  • yes. MEAN [Mongo Express Angular Node] stack. app.use(session({ saveUninitialized: true, resave: true, //cookie: { maxAge: 600 }, secret: config.sessionSecret, store: new mongoStore({ db: db.connection.db, collection: config.sessionCollection }) })); – Nandish Mar 04 '15 at 09:51
  • 4
    Don't post code in comments, and please, format your code in your question – Alex Mar 04 '15 at 09:55

1 Answers1

1

Better use Token based sessions, It will help you making you API universal ie, You can also use the same API for mobile applications. Try implementing Token Based Authentication using psJwt. You will find good articles for this on scotch.io and on Plural sight.

Vaibhav Jain
  • 2,155
  • 5
  • 27
  • 41