Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Express and Redis sessions don't survive browser close

Express and Redis sessions don't survive browser close

Problem

I am trying to get persistent sessions working using RedisStore but whenever i close the browser the session is destroyed. I tried setting the maxAge and ttl but nothing makes the session survive a browser restart. What am i doing wrong?

The relevant parts of my code are:

var app = express();
var RedisStore = require('connect-redis')(express);

app.use(express.cookieParser('my secret key'));
  app.use(express.session({
  store: new RedisStore({
    host: 'localhost',
    port: 6379,
    ttl: (60000 * 24 * 30),
    cookie: { maxAge: (60000 * 24 * 30) }
  }),
  secret: 'my secret key'
}));
Problem courtesy of: anges244

Solution

The cookie should belong in the object you pass to express.session, not in the object you pass to RedisStore.

Try the following:

app.use(express.session({
  store: new RedisStore({ host: 'localhost', port: 6379, ttl: (60000 * 24 * 30)}),
  cookie: { maxAge: (60000 * 24 * 30)},
  secret: 'my secret key'
}));
Solution courtesy of: canadaCoder

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Express and Redis sessions don't survive browser close

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×