mardi 24 février 2015

Multiple express session instances on first page load

I am automatically giving every user a session and storing the session data in a MongoDB collection called sessions. I am also setting a cookie which allows me to identify every (returning) user. When a user returns, I am successfully able to find my session when the cookie is sent with the request.


Everything works fine expect the very first time a user accesses the site. The front-end (which is built with Angular), makes six different calls simultaneously to the Node server (every call has its own purpose). Since there is no cookie set yet, the Node Server thinks there are six different users calling the server by which six sessions are created and stored to the sessions collections.


The next call (after a user action) happens with the second to last created session and each future call is made with this session id.


How can I solve the initial creation of six different sessions when the user accesses the site for the first time?


It is possible to perform one call to "initiate" the session and execute the rest after the response. But if there is another option, I would really like to hear it.


I am using a custom build of Express.js 4.0, Node.js v0.10.32 & MongoDB v2.6.5.


This is how I create my session and set the cookie:



app.use(session({
saveUninitialized: true,
resave: true,
cookie: { secure: false, httpOnly: false },
name: 'someName',
secret: 'someScret',
store: new mongoStore({
db: framework.mongoose.connection.db,
collection: 'sessions'
})
}));


If you need more information, please let me know.


Aucun commentaire:

Enregistrer un commentaire