Express creating new session for each request
See original GitHub issueExpress-session is creating a new session (new sessionID) for new request. And it happens intermittently, for example sometimes for first 3-4 hits it will work fine and keep using same existing session and on 4th hit it will create a new one, Sometimes it will create new session on first request itself.
Things that i have already tried and are not working…
Express session is created after static file routes. Express session secret and cookieParser secret are set to same. Using app.use(favicon(path.join(__dirname, ‘public’, ‘favicon.ico’))); for favicon. Setting very high value for max-age field while creating cookie. Below is my app.js snippet -
var passport = require('passport');
var expressSession = require('express-session');
var app = express();
app.engine('hbs', hbs.express4({
partialsDir: __dirname + '/views/partials'
}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
var flash = require('connect-flash');
app.use(flash());
var initPassport = require('./passport/init');
initPassport(passport);
//app.use(bodyParser.urlencoded());
app.use(cookieParser('mysecret'));
app.use(expressSession({ secret: 'mysecret', name:'user', cookie: {maxAge: 60000000}}));
app.use(passport.initialize());
app.use(passport.session());
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
node.js - Express-session creates new session every request
Turns out all I was missing was app.set('trust proxy', 1) in my setup and sameSite: 'none' in cookie: {} in session middleware.
Read more >Express session middleware
To store or access session data, simply use the request property req.session , which is (generally) serialized as JSON by the store, so...
Read more >Session Management in Node.js using ExpressJS and ...
Express -session - an HTTP server-side framework used to create and manage a session middleware. This tutorial is all about sessions.
Read more >How to Manage Session using Node.js and Express
Create a new folder and switch to it using the terminal. Run this command to create ... Now using 'request' variable you can...
Read more >Cookie and Session (II): How session works in express-session
Check if this request has cookie with valid session ID · If yes, search for the matching session ID in session store and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For those googling around for this same issue, I fixed mine by setting the flag
saveUninitialized: false
in the session middleware.I’ve encountered a similar problem, except it was because I wasn’t including cookies when using the browser fetch.