In Express Session Every Time new session id is generated on ajax call
See original GitHub issueI am Using expression-session and express-mysql-session for storing in database but on every ajax returns new session id.For more info calling from http only.This issue is not coming when i disable security of browser.``
My Code is
var express = require('express');
var mysql = require('mysql');
var jwt = require('jsonwebtoken');
var session=require('express-session');
var MySQLStore = require('express-mysql-session')(session);
var options = {
host: 'localhost',// Host name for database connection.
port: 3306,// Port number for database connection.
user: 'root',// Database user.
password: '',// Password for the above database user.
database: 'node',// Database name.
checkExpirationInterval: 900000,// How frequently expired sessions will be cleared; milliseconds.
expiration: 1512671400000,// The maximum age of a valid session; milliseconds.
createDatabaseTable: true,// Whether or not to create the sessions database table, if one does not already exist.
connectionLimit: 10,// Number of connections when creating a connection pool
schema: {
tableName: 'sessions',
columnNames: {
session_id: 'session_id',
expires: 'expires',
data: 'data'
}
}
};
var connection = mysql.createConnection(options); // or mysql.createPool(options);
var sessionStore = new MySQLStore({}/* session store options */, connection);
var router = express.Router();
router.use(session({
name: 'session_cookie_name',
secret: 'session_cookie_secret',
store: sessionStore,
resave: false,
saveUninitialized: true,
cookie: { path: '/', httpOnly: false, secure: false, maxAge: 365 * 24 * 60 * 60 * 1000 }
}));
router.get('/session', function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.send(req.sessionID);
});
Issue Analytics
- State:
- Created 6 years ago
- Comments:18 (10 by maintainers)
Top Results From Across the Web
Every time React sends request to Express, a new session is ...
Every time React sends request to Express, a new session is generated ... sessionID; // is generated each time React client send request, ......
Read more >New Session Id generated on every AJAX call whe... - JBoss.org
When I login to the portal with normal HTTP url, and make the AJAX call, the session Id for the AJAX request remains...
Read more >How to Manage Session using Node.js and Express
To handle concurrent session , you need to create unique Session ID and give it to client once the session is set so...
Read more >Cookie and Session (II): How session works in express-session
Session is created in server when the client send a request to it for the first time. ... Check if this request has...
Read more >Session | NestJS - A progressive Node.js framework
Once the installation is complete, apply the express-session middleware as ... used to sign the session ID cookie, while all the elements will...
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
Actually (for my situation at least) I’ve come across a solution for this right after I posted lol updated below:
So (with fetch at least) it doesn’t send cookies by default, you need to set credentials to “same-origin”. This kicks the express-session into gear to use the correct session for the request. @brijeshIOGit / @ricfernandes not sure if this helps? Maybe double check the request is actually sending the HTTP cookie header.
Thanks @ottis ! I’m going to close this since we have a solution 🎉