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
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({
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.
Steven Ventimiglia
Creative Technologist & Sr. Front-End Developer
Ipseeta Priyadarshini
Software Developer
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.