Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why connect-mongo creates new session for every request? #57

Closed
msmirnov opened this issue Mar 8, 2013 · 10 comments
Closed

Why connect-mongo creates new session for every request? #57

msmirnov opened this issue Mar 8, 2013 · 10 comments
Labels

Comments

@msmirnov
Copy link

msmirnov commented Mar 8, 2013

I have two nodejs servers (web-server, socket-server), that are connected to each other by socket.io. On web-service I use express.js and passport.js as authentication middleware.

This is my web-server config:

var express = require('express'),
    mongo = require('mongodb'),
    io = require('socket.io'),
    passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    MongoStore = require('connect-mongo')(express);
app.configure(function () {
    app.use(express.cookieParser());
    app.use(express.methodOverride());
    app.use(express.bodyParser());
    app.use(express.session({
        secret: 'keyboard cat',
        store: new MongoStore({
          db: 'MyDatabase'
        })
    }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(app.router);
    app.use(express.static(__dirname + '/htdocs'));
});

When I use connect-mongo, it creates a new session for each http-request:
(0 element creates with log in request, other elements are created, when I press F5).
mongovue

When web-server takes socket connection, connect-mongo creates new session. There are about 50 new documents per minute.

What could be the reason?

In the case of updating the page, helped tip to add app.use(express.favicon()).

With sockets question is still actual.

My socket-server code

function sendPortalJSON (portal_id, data, _event) {
    https.get({
        host : ...,
        port : ...,
        path : "/" + _event + "?data=" + encodeURIComponent( JSON.stringify (data))
    }).on('error', function (err) {

    });
}
...
sendPortalJSON(1, agent_data[i].d, "cpu-details");

And on web-server:

app.get('/cpu-details', function (req, res) {});
@kcbanner
Copy link
Collaborator

kcbanner commented Mar 8, 2013

Check the system clock of both your server and client, make sure they are the same. The client could be immediately expiring its cookies if it has a wrong time.

@QETHAN
Copy link

QETHAN commented Apr 14, 2013

app.use express.methodOverride()
app.use express.cookieParser()
app.use express.session
  secret:'pansafe_authserver@pansafe'
  store: new MongoStore
     db: 'pansafe_authserver'
  cookie: {
            maxAge: 24 * 360000 # 24 hours
        }

i got the same question. here i have a client with 127.0.0.1:3000, and a server with 127.0.0.1:8000, when second request, the session will be a new one without user info. i'm very confused!!! help me. TX

@QETHAN
Copy link

QETHAN commented Apr 14, 2013

app.use express.cookieParser()
  app.use express.session
    secret:'pansafe_authserver111@pansafe'
    store: new MongoStore
       db: 'pansafe_authserver111'
    cookie: {
            maxAge: 24 * 360000 # 24 hours
        }

the client one, the above is server conf

@kcbanner
Copy link
Collaborator

Check your system time on your server and client. If they are too far apart
cookies will be instantly expiring.
On Apr 14, 2013 10:40 AM, "Ethan" [email protected] wrote:

app.use express.methodOverride()
app.use express.cookieParser()
app.use express.session
secret:'pansafe_authserver@pansafe'
store: new MongoStore
db: 'pansafe_authserver'
cookie: {
maxAge: 24 * 360000 # 24 hours
}

i got the same question. here i have a client with 127.0.0.1:3000, and a
server with 127.0.0.1:8000, when second request, the session will be a
new one without user info. i'm very confused!!! help me. TX


Reply to this email directly or view it on GitHubhttps://github.com//issues/57#issuecomment-16352047
.

@QETHAN
Copy link

QETHAN commented Apr 14, 2013

client and server in the same mac, how the system time wil be not same. how to check system time. TX

@QETHAN
Copy link

QETHAN commented Apr 15, 2013

work out. caused by the same ip '127.0.0.1' for both

@QETHAN
Copy link

QETHAN commented Apr 20, 2013

the truth is cookie kept by chrome. the key is localhost for cookie when you have 2 web server with the same ip localhost. so i add a key param for express.session : key:'xxxxx', same cookie but different session every request. it works perfect!

@eranimo
Copy link

eranimo commented May 7, 2013

Please explain what you did. I am having the same issue.

@marshallswain
Copy link

Check out the Express API docs here: http://expressjs.com/api.html#cookieSession

The default cookie name is connect.sess. If you don't pass a custom key value to cookieSession(), it will use that name. Do that on two servers and they will overwrite each others' cookies. Very frustrating if you can't figure out what's happening!

_example:_
app.use(express.cookieSession({key:"MyFancyKeyName"}));

nukulb added a commit to hubba/connect-mongo that referenced this issue May 2, 2014
connect-mongo stores every ping as a session. This obviously bleeds and makes
the session store massive with time. Obviously we can write a service to clean
it up but we have chosen to work around it using this hack

Once this issue is fixed we can remove it
jdesboeufs#57
@Unitech
Copy link

Unitech commented Jun 30, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants