Session: express-session won't set cookie connect.sid

Created on 6 Feb 2017  路  3Comments  路  Source: expressjs/session

hi ,
I can't figured out why express-session won't set connect.sid cookie on my app. I'm noob to node so apologize if i missed something

Here is my simple server side app :

var express         = require('express')
   , app            = express()
   , http           = require('http').Server(app)
   , socketIo       = require('socket.io')(http)
   , cookieParser   = require('cookie-parser')
   , cookie         = require('cookie')
   , expressSession = require('express-session')
   , port           = 3000
   , helmet         = require('helmet')
   , sessionStore   = new expressSession.MemoryStore({ reapInterval: 60000 * 10 })
   , sessionSecret  = 'keyboard cat'
   ;

    /** Configuration */
    app.disable('x-powered-by');
    app.use(helmet());
    app.set('trust proxy', 1)
    app.use(cookieParser());
    app.use(expressSession({
        'secret': sessionSecret,
        'store' : sessionStore,
        'resave': false,
        'saveUninitialized': false,
    }));

    socketIo.use(function(socket, next) {
        console.log( socket.request.headers.cookie );
        // returns (value for example)=>io=aMH75hn6NBZoqeJTAAAA 
        var cookies = cookie.parse(socket.request.headers.cookie);
        console.log( 'cookies ' + cookies );
        // returns => cookies [object Object]
        console.log( 'cookies["connect.sid"] = ' + cookies['connect.sid'] );
        // returns => cookies["connect.sid"] = undefined
        var sessionID = cookieParser.signedCookie(cookies['connect.sid'], sessionSecret);
        sessionStore.get(sessionID, function(err, session) {
            console.log( session );
            //returns => undefined
            if ( session ) {
                console.log( 'Authentified' );
                return next();
            } else {
                console.log( 'error Not authentified' );
                return next(new Error('Not Authenticated'));
            }
        });
    });

    socketIo.on('connection', function (socket) { // New client
        console.log( 'new connection..' );
    });

    /** Start server */
    http.listen(port);
    console.log( "listening on :" + port );

As you can see connect.sid is undefined, i tried many way few days ago but without success.. this morning magical connect.sid was inputted correctly through console but..after clearing cache, connect.sid wasn't set and still undefined.. How explain that ?

I use a VM based on linux, express 4.14.1 and express-session 1.15 with nodejs 7.5.0

question

Most helpful comment

Hi @inkquery I don't see any routes on your Express app, which means that since you also put 'saveUninitialized': false, no cookie will get set since there is nothing causing the cookie to ever get set.

All 3 comments

Hi @inkquery I don't see any routes on your Express app, which means that since you also put 'saveUninitialized': false, no cookie will get set since there is nothing causing the cookie to ever get set.

did you solve this? I'm also having this problem

did you solve this? I'm also having this problem

Was this page helpful?
0 / 5 - 0 ratings

Related issues

UnderTheMoonspell picture UnderTheMoonspell  路  3Comments

darrenscerri picture darrenscerri  路  3Comments

yolapop picture yolapop  路  3Comments

haxxxton picture haxxxton  路  4Comments

theonlygusti picture theonlygusti  路  4Comments