Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Node/Express app not recognizing Session store

I have an extremely small express app to illustrate a problem I'm having.

I'm using connect-redis as a session store on my express app. I'm having a problem simply connecting to it though. Simply printing out req.session.store results in undefined as shown below:

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

const isDev = process.env.NODE_ENV !== 'production'

app.use(session({
  store: new RedisStore({
    host: 'localhost',
    port: 6379
  }),
  secret: 'super-secret-key', // TBD: grab from env
  resave: false,
  saveUninitialized: false,
  cookie: {
    maxAge: 1000 * 60 * 60 * 24,
    secure: !isDev, // require HTTPS in production
  }
}))

app.get('/', (req, res) => {
  console.log('**testing**')
  console.log(req.session.store)
  res.send('rendering this text')
})

app.listen(3001, () => {
  console.log('listening on 3001')
})

The output of this is:

listening on 3001
**testing**
undefined

In the store property, I've tried connecting via the url property and the client property in addition to the current host and port method.

I'm not seeing any errors either so I'm not sure why the store is always undefined.

What am I missing with this?

also, I did start redis using redis-server and I am able to connect to it through other clients


Update 1
Also, I did look at this StackOverflow which some found useful. The issue is, from version 1.5> of express-session, it says you don't need the cookie-parser module, so I don't believe that's the issue.

like image 216
qarthandso Avatar asked Dec 23 '22 02:12

qarthandso


1 Answers

req.session will only be having properties that are set to the session, you have never set a session property named store. below is an example code of a working RedisStore sessions

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

const redis = require('redis');
const client = redis.createClient({
  host: '127.0.0.1', 
  port: 6379
});
client.on('error', function (err) {
  console.log('could not establish a connection with redis. ' + err);
});
client.on('connect', function (err) {
  console.log('connected to redis successfully');
});

app.use(session({
  store: new RedisStore({client: client}),
  secret: 'super-secret-key',
  resave: true,
  saveUninitialized: true,
  cookie: {
    ttl: 30 * 10000,
    httpOnly: true,
    secure: false
  }
}));

app.get('/', (req, res) => {
  client.get('user', (err, data) => {
    console.log('data in redis:', data)
  });
  res.status(200).send('OK');
});

app.get('/login', (req, res) => {
  if (req.session.user) {
    res.redirect('/')
  } else {
    client.set('user', JSON.stringify({username: 'Adeeb'}))
  }
  res.send('login');
})

app.get('/logout', (req, res) => {
  req.session.destroy();
  res.send('logout');
});

app.listen(3001, () => {
  console.log('server running on port 3001')
})
like image 155
Adeeb basheer Avatar answered Dec 26 '22 02:12

Adeeb basheer