Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean all sessions in Nodejs (heroku + redis)?

I'm using nodejs (expressjs) hosted on heroku.

Sessions are stored in redis (Redistogo plugin for heroku):

RedisStore = require('connect-redis')(express)
app.use express.session
      secret: process.env.CLIENT_SECRET
      cookie: { maxAge: 604800000 }
      store: new RedisStore {client: redis}

After user logged in I store his info in req.session

after_user_logged_id = (req, user)->    
  req.session.current_user =
    id: user._id
    name: user.name

I need to restart server and clean all sessions: logout all users to force them login one more time. How should I do this ? Restarting redis plugin doesn't help.

like image 470
Oleg Dats Avatar asked Feb 05 '13 13:02

Oleg Dats


1 Answers

redis.flushdb() may be a bit extreme if you end up storing something else in redis. You just need to delete all the "sess:*" keys. Unfortunately, there is no del *, so you can use something like this:

redis.keys("sess:*", function(err, key) {
  redis.del(key, function(err) {
  });
});
like image 113
Pascal Belloncle Avatar answered Sep 18 '22 12:09

Pascal Belloncle