Express-Session not persisting / Working in production even having a store option in the session options
View other answers to this thread3.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
Creative Technologist & Sr. Front-End Developer
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
Example:
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({
uri: MONGODB_URI,
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
app.use(
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.
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!
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