Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

feat: allow setting custom session cookie expiration#1156

Merged
eliandoran merged 8 commits intodevelopfrom
feat_custom_cookie-session-expiration
Feb 13, 2025
Merged

feat: allow setting custom session cookie expiration#1156
eliandoran merged 8 commits intodevelopfrom
feat_custom_cookie-session-expiration

Conversation

@pano9000
Copy link
Copy Markdown
Contributor

@pano9000 pano9000 commented Feb 10, 2025

Hi,

this PR is a feature on top of #1155 – which is why you see some extra commits here.

It will allow users to set their own session expiration, e.g. currently it defaults to 1 day – but I can see users who might want to limit it to a shorter period of time (myself included ;-)).

it also set the TTL of the FileStore for the session to the same value as the maxAge from the cookie.
Before that it was set to 30 days, which (if I am not completely mistaken) would've left a couple of dead "unused" session in the FileStore.

(this still needs some testing "in the field", which is why it is set to draft)

@pano9000 pano9000 force-pushed the feat_custom_cookie-session-expiration branch from 56b407c to 6a97d6f Compare February 11, 2025 08:46
@pano9000
Copy link
Copy Markdown
Contributor Author

pano9000 commented Feb 11, 2025

kindly ignore my last WIP commit – this will be replaced by the final commit, after my testing is done, on where we set the maxAge

allows users to control how long their session will be live, before it expires and they are forced to login again

defaults to 1 day  ("24 * 60 * 60 * 1000") as previously set in sessionParser
…ion cookies maxAge

this avoids having "unused" dead session on the filesystem
…lt value to 21 days

21 days was used in the login route previously, when "remember me" was set
cookie will use the default value set in sessionParser middleware, which is controlled by
config.Session.cookieMaxAge

if rememberMe is not set -> the value is unset and the cookie becomes a non-persistent cookie,
which the browser delete after the current session (e.g. when you close the browser)
previously it was either a number like string (in case env or config.ini was used) or a number (the fallback value)

we now parseInt the value -> if any value is NaN (e.g. because it was incorrectly set) it will try with the next, before it uses the fallback value

the strange looking `parseInt(String(process.env.TRILIUM_SESSION_COOKIEMAXAGE))` is required to make TypeScript happy, other variants of trying to get the value into a string were not good enough for typescript :-)

The `String(process.env.TRILIUM_SESSION_COOKIEMAXAGE)` will now either return a number like value or 'undefined' (as string), which parseInt parses into NaN, which is falsy.
@pano9000 pano9000 force-pushed the feat_custom_cookie-session-expiration branch from 6a97d6f to b692c00 Compare February 13, 2025 08:47
@pano9000 pano9000 marked this pull request as ready for review February 13, 2025 08:48
@pano9000
Copy link
Copy Markdown
Contributor Author

changes done as discussed, we now use Seconds to enter the value and we now only set the default value once, instead of in two places:

I've went with setting the default value in sessionParser, made more sense to me to have it "close" to the configuration, instead of being set in the login route

@eliandoran eliandoran merged commit bfd894b into develop Feb 13, 2025
5 checks passed
@eliandoran eliandoran deleted the feat_custom_cookie-session-expiration branch February 13, 2025 19:50
pano9000 added a commit that referenced this pull request Apr 15, 2025
…okieMaxAge

regression introduced with #401
custom cookieMaxAge feature added with #1156

fixes #1709
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants