3

To date I have been using the default MemoryStore for my Express sessions. Everything has been working great, except that all session data is lost between restarts. So I am trying to get my sessions working with Redis & Connect-Redis instead.

Here is the part of my code that concerns session config with Redis:

var express = require( "express" ),
    app = module.exports = express(),
    passport = require( "./passport" ),
    http = require( "http" ),
    RedisStore = require( "connect-redis" )( express ),
    redis = require( "redis" ).createClient();

app.configure(function(){

    app.set( "passport", passport );
    app.use( express.cookieParser());
    app.use( express.session({
         secret: "I can haz working sessions?",
         store: new RedisStore({ client: redis })
    }));
    app.use( passport.initialize() );
    app.use( passport.session() );
    ...

When I run this application, there are no errors. However when I try to login, although it performs the initial login, I just keep getting bounced back to the login screen. It is as if passport cannot set a cookie, or cannot read a cookie after it has been set.

For reference, here are the functions I use in passport to serialize / deserialize the session:

passport.serializeUser(function( user, done ) {
    done( null, {
        id : user.id,
        type : user.type,
        firstname: user.firstname,
        lastname: user.lastname
    });
});

passport.deserializeUser(function( user, done ) {
    done( null, user );
});

Any advice on where I am going wrong? Do the passport serializeUser / deserializeUser need to be modified to work with redis? Or am I missing something else?

Thanks (in advance) for your help.

3
  • 1
    Check your browser's development tools to see if a cookie is actually set (and also sent back to the server by the browser). Your code seems okay.
    – robertklep
    Mar 22, 2013 at 6:30
  • Sounds like same problem that I have with cookie sessions: stackoverflow.com/questions/19011531/… Sep 26, 2013 at 7:36
  • @user1031947 : Were you able to solve your problem ? Please update.
    – vivekanon
    Mar 16, 2015 at 14:15

1 Answer 1

0

One possibility: maybe your app can't connect to Redis? I found that when my redis-server was not running, I experienced the symptom you describe (silently fails to set cookies).

Aside from checking that it's running, make sure your Node server can actually connect to it—that is, that there are no firewall rules keeping you out.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.