koajs / generic-session

koa session store with memory, redis or others.
MIT License
414 stars 65 forks source link

feat: allow TTL to be set dynamically #140

Closed ejose19 closed 2 years ago

ejose19 commented 2 years ago

Currently, it's very common for websites to offer a "dynamic" session age ("remember for X/Y days", or disabling "remember" which wipes the cookie after browser is closed), however with the current implementation it's not possible to control TTL per request (for example, if cookie will have a maxAge !== null, use it, else use a default value). After this PR, consumers accomplish this result as this:

{
  ttl: (session) => {
    if (session.cookie?.maxAge === null) {
      return 1000 * 60 * 60 * 24;
    }
  }
}

it would allow browser-session cookies to be removed after 1 day from the store and not live indefinitely like they do now.

niftylettuce commented 2 years ago

If you can PR to update the README too @ejose19? Maybe add this example too?

ejose19 commented 2 years ago

Sure

niftylettuce commented 2 years ago

As soon as README updated I will version release! Thank you for this excellent PR!

niftylettuce commented 2 years ago

v2.3.0 released to npm, thank you again! 🙏 🎉

https://github.com/koajs/generic-session/releases/tag/v2.3.0

ejose19 commented 2 years ago

@niftylettuce thanks to you as well for your prompt review!