modernator
modernator

Reputation: 4427

Express session using redis store couldn't create session

I'm using Express 4.13.1 with Redis as session store.

app.use(session({
    store: new RedisStore,
    secret: '...',
    resave: false,
    saveUninitialized: true
}));

It worked at first, but then it didn't work (after upgrading node.js). Using default store(memory) works fine, but using RedisStore, session is not being created. This is the code to create session :

router.post('/admin/login', (req, res, next) => {
    Auth.compare(..., () => {
        req.session.authorized = true;
        req.session.userId = id;

        console.log(req.session);    // It prints well. Session is created

        res.redirect('/admin/main');
    });
});

at that moment, session was changed:

Session {
  cookie: 
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true },
  authorized: true,
  userId: '...'}

but after that, session wasn't created:

router.get('/admin/main', (req, res, next) => {
    console.log(req.session);
    ...
});

printing session:

Session {
  cookie: 
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true }
}

I used session.save manually, but it didn't work. What am I missing?

Upvotes: 2

Views: 1518

Answers (1)

FurkanO
FurkanO

Reputation: 7308

While working with redis one might check a few points in the app to solve a problem like this. First of all, check that you browser gets the cookie, for instance by checking resources -> cookies on browser.

  • Check the expiration date and see the session id there.

Secondly, check the redis for the corresponding key, if key exists and the expiration of key from redis-cli :

  • All keys : keys *

  • Value by key : get key (sess:PMlu4s1PYHnSZXBMSoJGLgjNSGXq_s-g is a key for me)

  • Check when your key expires : ttl key

There might be one of the following problems :

  • You might have configured redis inappropriately
  • You might have configured redis ttl inappropriately, for instance your ttl value should be in seconds, whereas cookie maxAge is in miliseconds.

Instead of asking for your server.js code, I recommend you to follow these steps to check if everything is okay with expiration or at least session is created .

Upvotes: 2

Related Questions