Thomas Bormans
Thomas Bormans

Reputation: 5372

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.

Upvotes: 1

Views: 1972

Answers (2)

HDK
HDK

Reputation: 814

var session = require('express-session');
    app.use(session({
            secret: 'a4f8071f-c873-4447-8ee2',
            cookie: { maxAge: 2628000000 },
            store: new (require('express-sessions'))({
                storage: 'mongodb',
                instance: mongoose, // optional
                //host: 'localhost', // optional
                //port: 27017, // optional
                //db: 'test', // optional
                collection: 'sessions', // optional
                expire: 86400 // optional
            })
        }));

use express-session and express-sessions

Upvotes: 0

Thomas Bormans
Thomas Bormans

Reputation: 5372

I figured it out myself. There was an issue with OPTIONS requests. Options requests do not send the cookie in their headers which caused the creation of new sessions on the Node server.

Since options requests are only used for checking for authorization for cross-domains, it must have been something with cors. I disabled cors and started Chrome with cors disabled. This solved the problem.

Please note: I only implemented cors for (easier) local development. I do not require cors on the live server.

This was the commando (Mac only) to open Google Chrome:

open -a Google\ Chrome --args --disable-web-security

Upvotes: 2

Related Questions