Skip to content

Commit

Permalink
Add TTL interception for external conditional TTL handling (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
movwf authored May 11, 2023
1 parent 54d17d3 commit 8d385c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface RedisStoreOptions {
prefix?: string
scanCount?: number
serializer?: Serializer
ttl?: number
ttl?: number | {(sess: SessionData): number}
disableTTL?: boolean
disableTouch?: boolean
}
Expand All @@ -31,7 +31,7 @@ class RedisStore extends Store {
prefix: string
scanCount: number
serializer: Serializer
ttl: number
ttl: number | {(sess: SessionData): number}
disableTTL: boolean
disableTouch: boolean

Expand Down Expand Up @@ -181,6 +181,10 @@ class RedisStore extends Store {
}

private _getTTL(sess: SessionData) {
if (typeof this.ttl === "function") {
return this.ttl(sess)
}

let ttl
if (sess && sess.cookie && sess.cookie.expires) {
let ms = Number(new Date(sess.cookie.expires)) - Date.now()
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ If the session cookie has a `expires` date, `connect-redis` will use it as the T

Otherwise, it will expire the session using the `ttl` option (default: `86400` seconds or one day).

```ts
interface RedisStoreOptions {
...
ttl?: number | {(sess: SessionData): number}
}
```

`ttl` also has external callback support. You can use it for dynamic TTL generation. It has access to `session` data.

**Note**: The TTL is reset every time a user interacts with the server. You can disable this behavior in _some_ instances by using `disableTouch`.

**Note**: `express-session` does not update `expires` until the end of the request life cycle. _Calling `session.save()` manually beforehand will have the previous value_.
Expand Down

0 comments on commit 8d385c2

Please sign in to comment.