Custom way of session rolling
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Active
- Tech stack
- express, javascript, node.js, postgresql
Research direction
No repository file or test is named; start by reproducing the middleware with the connect-pg-simple store and trace whether req.session.touch() reaches the store. Done means identifying why the expiration is not extended and determining the compatible behavior for this session setup.
Written by the indexing model from the issue text.
Description
My goal is to extend the session expiration when 2/3 of its duration has passed instead of doing this on every request.
For that, I've kept the rolling option disabled and I've written a custom middleware as follows:
export const sessionTouchMiddleware = (req: Request, _res: Response, next: NextFunction) => {
if (!req.session.userId || !req.session.cookie) {
return next()
}
const { originalMaxAge, expires } = req.session.cookie
if (!originalMaxAge || !expires) {
return next()
}
const sessionStart = expires.getTime() - originalMaxAge
const elapsedMs = Date.now() - sessionStart
// Touch the session if two-thirds of its lifespan elapsed
if (elapsedMs >= originalMaxAge * (2 / 3)) {
req.session.cookie.maxAge = originalMaxAge
req.session.touch()
}
next()
}
The above has no effect. I'm using connect-pg-simple store. Any help is appreciated.
- Dominant language
- JavaScript
- Stars
- 6.4k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from expressjs/session
-
future
Difficulty 1/5 Under an hour Newbie friendliness 62/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
discuss
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
discuss v2
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
question
Difficulty 5/5 Over a week Newbie friendliness 25/100
All issues in expressjs/session
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·