Edge case: Unable to login with 2 session cookies stored in browser
- Dominant language
- JavaScript
- Stars
- 6.4k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
When I reconfigured session storage to share cookie/session between subdomains (added {domain: ".domain.com"}
I have encountered on multiple time the issue where user ended up with old and new session cookie in the browser.
when express-session is getting cookies it was always returning the older session id.
In this case following code is executed:
```
// generate the session object
debug('fetching %s', req.sessionID);
store.get(req.sessionID, function(err, sess){
// error handling
if (err && err.code !== 'ENOENT') {
debug('error %j', err);
next(err)
return
}
try {
if (err || !sess) {
debug('no session found')
```
It is an edge case but express session behaves inconsistently. It creates/writes one session id and it reads the other id.
It creates conditions where it's not possible to log in without end user cleaning up their cookies.
It creates following loop
1. User tries to log in.
2. Express session gets incorrect session id from header
3. session storage returns null because session expired
4. Express session creates new session id (user is not logged in)
5. User is returned to login page ..
The way I resolved it was that I implemented middleware where I manually parse cookie header , detect multiple session cookies and expiring them manually.
In the above code snippet I would suggest to cleanup req.sessionID cookie in the case sess is not found in the session store.
Contributor guide
Assessment
This issue has not been assessed yet.