My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Express-Session not persisting / Working in production even having a store option in the session options

Aniketh Saha's photo
Aniketh Saha
·Feb 18, 2019

I saw many post regarding this issue but couldn't get the answer. In many posts the answers are to have a session storage.

In my project I am having a session - storage but still the problem persist

I am using connect-session-firebase and Firestore for storing the session but still I am not getting a persistent session.

It's working fine in development but not in Production even having a sessionstorage

My codes server.js

const session = require('express-session')
const cors = require('cors')
const helmet = require('helmet')
const { port , sessionSecretKey} = require('./configs/config')
var cookieParser = require('cookie-parser')
var flash = require('connect-flash');

const { store} = require('./configs/sessionStorage/firebaseSessionStorage')
const {dbname,MONGODB_URL,sessionKeys} = require('./configs/config.js')
//database connection
mongoose.connect(MONGODB_URL,{
  useNewUrlParser: true
});
const app = express();

app.use(helmet());

app.use(bodyParser.urlencoded({extended: true, limit: '50mb'}))
app.use(bodyParser.json({limit: '50mb'}))

app.use(cookieParser());

app.use(session({
  store,
  secret: sessionSecretKey,
  resave: true,
  saveUninitialized: true,
  cookie: {
    secure: process.env.NODE_ENV == "production" ? true : false ,
    maxAge: 1000 * 60 * 60 * 24 * 7
  }
}));

Now I have created a middleware with every request just to count the view using session

app.use(function(req,res,next){

  res.locals.user = req.user || null;
  if(req.session.views){
    req.session.views += 1
    req.session.save();
  }else{
    req.session.views = 1
    req.session.save();
  }
  console.log('req.session.views', req.session.views)
  next();
})

Its Giving output req.session.views 1 in production

And also when authenticating a user it is not able to persist the user. Even the firestore collection i.e the session database is filling with sessions in production and development both but still the session is not persisting

Thanks For Your Time