expressjs / expressjs/session

Can't set manual sessionID

Open
#158 17 comments 4 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
6.4k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

Similar to #148, I'm can't seem to set a manual `sessionID`. Setting it in `signedCookies` like seems to be the solution doesn't seem to work:

``` js
app
.use(function(req, res, next) {
if(req.query.sessionID) {
req.signedCookies["connect.sid"] = req.query.sessionID;
}
next();
})
.use(session({
httpOnly: false,
secret: 'secret',
resave: false,
saveUninitialized: true
})
.get('/session', function(req, res) {
console.log('sessionID:', req.sessionID);
req.session.tmp = req.session.tmp + 1 || 0;
req.session.save();
res.send('session: ' + req.session.tmp);
})
```

When I hit `http://localhost:3000/session?sessionID=42` a couple of times, the counter goes up. When I hit that exact same url in a different browser I expect it to pick up the count from the session, but it does not.

An observation: The log for `req.sessionID` is not what I gave to the query string, so I guess it's initialising its own sessionID instead.

I've also tried a custom `genid` like so:

``` js
.use(session({
httpOnly: false,
secret: 'secret42',
resave: false,
saveUninitialized: true,
genid: function(req) {
return req.query.sessionID || uuid.v1();
}
})
```

This seemed like a cleaner solution anyway, but it also doesn't work for me. It sets the `sessionID` correctly for the first hit, but it won't update it for requests with a different `sessionID`.

Any help on this would be great!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.