After setting up my code as shown below, I noticed that sessions are being persisted and the page is able to count the number of visits.
app.set('trust proxy', true)
// The documentation specifies '1' instead of 'true'
app.use(session({
secret: 'my secret',
proxy: true,
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}))
app.listen(3000, function(){
console.log("Server is connected!");
});
app.get("/login", (req, res) => {
if(req.session.page_views){
req.session.page_views++;
res.send("You visited this page " + req.session.page_views + " times");
} else {
req.session.page_views = 1;
res.send("Welcome to this page for the first time!");
}
});
However, when I removed the app.listen(3000, ...)
and opted to run on localhost
by executing firebase serve
in the CLI, the sessions were no longer persisted.
I also attempted deploying to a production environment using firebase deploy
, but unfortunately, the sessions were still not persisted.
I have made several adjustments within the app.use(session({
section and I believe the solution lies within those changes.
Any suggestions?
UPDATE
const express = require('express');
const session = require('express-session');
const FirestoreStore = require('firestore-store')(session);
const bodyParser = require('body-parser');
app.use(cookieParser('My secret'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
store: new FirestoreStore({
database: firebase.firestore()
}),
secret: 'My secret',
resave: true,
saveUninitialized: true,
cookie: {maxAge : 60000,
secure: false,
httpOnly: false }
}));