feat(core): add expirationStrategy support for stateful sessions - #248
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
📝 WalkthroughWalkthroughSession expiration settings move from nested JWT configuration to session-level configuration. Core types, expiration calculations, renewal flows, validation, tests, and documentation are updated. Documentation navigation redirects ChangesSession expiration configuration
Documentation navigation
Generated route tree formatting
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant getSession
participant calcExpiration
participant SessionsAdapter
getSession->>calcExpiration: calculate expiration outcome
calcExpiration-->>getSession: return expiration result
getSession->>SessionsAdapter: persist expiration and lastActivityAt
Possibly related PRs
Suggested labels: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 7
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docs/src/content/docs/`(core)/configuration/session-strategies.mdx:
- Around line 34-36: The session strategy table’s expirationStrategy entry omits
supported values. Update the type and description in the expirationStrategy row
to include fixed, rolling, absolute, and sliding, matching SessionConfigBase and
the documented section below while preserving the existing default.
In `@packages/core/src/`@types/session.ts:
- Around line 168-190: Align SessionConfigBase.expirationStrategy with the
runtime default by documenting and declaring the unset value as "absolute"
rather than "fixed". Update the stateless and stateful session configuration
resolvers to use the same "absolute" fallback whenever expirationStrategy is
omitted.
- Around line 220-223: Update StatefulStrategyConfig and its runtime consumers
to preserve the old session configuration alias, marking it deprecated and
defining explicit precedence over database when both are provided; ensure
deleteStrategy and maxSessions are read from the selected configuration.
Alternatively, document this as a breaking change and update all runtime config
consumption accordingly.
In `@packages/core/src/session/stateful/getSession.ts`:
- Around line 134-144: Update the renewal logic around updateExpires in the
stateful session flow to enforce sessionConfig.maxDuration from
session.createdAt: cap expiresAt at the createdAt-based maximum, and skip the
adapter.updateSession renewal once that cap has been reached. Preserve the
existing expiry calculation and lastActivityAt update for sessions still within
the allowed duration.
- Around line 139-144: Update the renewal logic in getSession around
adapter.updateSession so persistence failures are handled separately from
session validation failures: preserve the already-valid session and existing
expiry when the renewal write fails, and only expose the renewed expiresAt after
updateSession succeeds. Prevent renewal errors from reaching the outer catch
that returns session: null and clears the cookie.
In `@packages/core/test/config/session.test.ts`:
- Line 51: Update the migration test fixtures at the referenced session
configurations to set expirationStrategy directly on session instead of
session.jwt.expirationStrategy. Ensure these tests exercise the current
session-level configuration path, while retaining a separate fixture only if
needed to cover deprecated fallback compatibility.
In `@skills/security-practices/SKILL.md`:
- Around line 200-202: Replace the session-level maxExpiration option with
maxDuration in the hardened configuration, preserving the existing 30-day value
and surrounding session settings.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: c2c173e1-68ad-4b60-ba82-5371cd1b66fc
📒 Files selected for processing (16)
apps/tanstack-start/src/routeTree.gen.tsdocs/next.config.tsdocs/proxy.tsdocs/src/components/home/call-to-action.tsxdocs/src/content/docs/(core)/api-reference/server/createAuth.mdxdocs/src/content/docs/(core)/configuration/session-strategies.mdxpackages/core/CHANGELOG.mdpackages/core/src/@types/adapter.tspackages/core/src/@types/session.tspackages/core/src/jose.tspackages/core/src/session/stateful/getSession.tspackages/core/src/session/stateless/getSession.tspackages/core/src/session/stateless/refreshSession.tspackages/core/test/config/session.test.tsskills/create-auth/reference/create-auth.mdskills/security-practices/SKILL.md
💤 Files with no reviewable changes (1)
- docs/proxy.ts
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/src/session/stateless/getSession.ts`:
- Around line 29-35: Update the expiration handling in getSession around
calcStatelessExpiration so action === "invalid" returns { session: null, headers
} instead of the authenticated session; preserve the existing session return for
only "no_change" and "touch". Add a regression test covering a signed token
without an exp claim.
In `@skills/security-practices/SKILL.md`:
- Around line 230-240: Update the expiration design guidance around
`maxDuration` and `expirationStrategy` to state that `SessionConfigBase`
requires `maxDuration` for `absolute` and `sliding` strategies, and that it
bounds sliding-session renewal to prevent indefinite extension. Also recommend
setting `maxDuration` for `rolling` sessions.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 255dfca7-a196-4cf0-8e44-a59209ad27c9
📒 Files selected for processing (11)
docs/src/content/docs/(core)/configuration/session-strategies.mdxpackages/core/src/@types/index.tspackages/core/src/@types/session.tspackages/core/src/session/stateful/getSession.tspackages/core/src/session/stateless/getSession.tspackages/core/src/shared/assert.tspackages/core/src/shared/logger.tspackages/core/src/shared/utils/session-strategy.tspackages/core/test/api/stateless/getSession.test.tspackages/core/test/config/session.test.tsskills/security-practices/SKILL.md
🚧 Files skipped from review as they are similar to previous changes (3)
- packages/core/test/config/session.test.ts
- packages/core/src/session/stateful/getSession.ts
- docs/src/content/docs/(core)/configuration/session-strategies.mdx
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/CHANGELOG.md`:
- Around line 13-14: Update the stateful session flow in getSession and its
verifyDebounceLastActivity call to pass sessionConfig.touchInterval converted to
milliseconds, so touchSession uses the configured debounce interval instead of
DEFAULT_TOUCH_THRESHOLD_MS. Add a test using a non-default touchInterval that
verifies both the default debounce behavior and the configured interval
behavior.
In `@packages/core/src/shared/assert.ts`:
- Around line 273-275: Update isInvalidSlidingThreshold to reject NaN and other
non-finite numeric values while preserving the existing 0–1 range validation for
finite numbers. Add a regression test alongside the session configuration tests
covering slidingThreshold: NaN through the relevant validation path.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: eee89bb0-1e39-4895-a7fb-0a911036a5f8
📒 Files selected for processing (8)
packages/core/CHANGELOG.mdpackages/core/src/@types/session.tspackages/core/src/session/stateful/getSession.tspackages/core/src/session/stateless/getSession.tspackages/core/src/shared/assert.tspackages/core/src/shared/errors.tspackages/core/src/shared/utils/session-strategy.tspackages/core/test/config/session.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
- packages/core/src/shared/utils/session-strategy.ts
- packages/core/src/session/stateful/getSession.ts
- packages/core/src/@types/session.ts
Description
This pull request adds support for configurable session expiration strategies in the Stateful session strategy and unifies session lifetime configuration across both Stateless (JWT) and Stateful (Database) strategies.
Previously, options such as
maxAge,maxExpiration, andexpirationStrategywere only available under thejwtconfiguration, making them specific to the Stateless strategy. With this change, session lifetime is now configured at thesessionlevel, allowing both session strategies to share the same configuration model while retaining strategy-specific options.In addition, this PR introduces database-specific options for managing activity updates and session renewal, giving Stateful sessions more control over expiration and last-activity tracking.
Key Changes
expirationStrategysupport for the Stateful session strategy.maxAge,maxDuration(formerlymaxExpiration), andexpirationStrategyto the top-levelsessionconfiguration.touchIntervalfor controlling how frequently Stateful sessions update their last activity timestamp.jwt.maxAge,jwt.maxExpiration, andjwt.expirationStrategy.Usage
Warning
The following options have been deprecated under the
jwtconfiguration and will be removed in a future major release:jwt.maxAgejwt.maxExpirationjwt.expirationStrategyUse the equivalent options under the top-level
sessionconfiguration instead.Related PRs
@coderabbitai ignore