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

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
  useNewUrlParser: true
const app = express();


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


  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


  res.locals.user = req.user || null;
    req.session.views += 1;
    req.session.views = 1;
  console.log('req.session.views', req.session.views)

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

Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown 路 Publish articles on custom domain 路 Gain readership on day zero 路 Automatic GitHub backup and more

Steven Ventimiglia's photo

Aniketh - The session needs to be stored in order to persist, which you already seem to have part of. For MongoDB, I use:

npm i connect-mongodb-session --save

const mongoose = require('mongoose');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);

const MONGODB_URI = 'mongodb://localhost:27017/[database_name]';

const app = express();
const store = new MongoDBStore({
  collection: 'sessions'

// define database via mongoose
mongoose.connect(MONGODB_URI, {
  useNewUrlParser: true
mongoose.set('useCreateIndex', true);

// connect to mongodb
const conn = mongoose.connection;
conn.on('error', console.error.bind(console, '[!] Connection Error '));

// session
    secret: 'my secret',
    resave: false,
    saveUninitialized: false,
    store: store

Now, when you check the database, you'll notice db.collection.session.

I use Browsersync in my Gulp pipeline, and needed to learn how to retain the session along with that and nodemon. In my case, this was the solution.

Show +1 replies
Steven Ventimiglia's photo

Unfortunately, I'm not familiar with Firebase. However, Sukant Kumar is, so maybe he can assist.

If you do figure out a solution, let us know, so other people won't hit the same issue. Good luck!

Aniketh Saha's photo

Steven Ventimiglia I am quite sure that problem is not with firebase. The problem is that session persistence is not working in production even though having a session-storage

I will surely message him regarding this. Yeah sure I will post about this as soon as I get the solution

Ipseeta Priyadarshini's photo

Hey Aniketh Saha I can't say much about firebase but I was facing the same problem with Redis session store. In production, the disk persistence was on, but Redis failed to save snapshots. As a result, all the writes were stopped and sessions couldn't be created. So, I suggest that you verify if Firebase is actually able to store data.

Aniketh Saha's photo

Yeah its storing something in firebase with a collection name session . And there are huge number of records in that collection . I am using the same firebase store for development its working. Idk why its not working for production