[GH-ISSUE #539] Incorrect Session Expiration Calculation with Secondary Storage #16942

Closed
opened 2026-04-15 14:53:50 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @wh5938316 on GitHub (Nov 15, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/539

@Bekacru

When enabling secondary storage, the session expiration period is significantly shortened. The root cause lies in the following issue: the code.

The Date.now() function returns the value in milliseconds, while the sessionExpiration variable is in seconds. The code incorrectly adds these two values directly without converting sessionExpiration to milliseconds using getDate(sessionExpiration, "ms"). This results in session expiration being 1/1000th of the intended duration when secondary storage is enabled.

I have submitted a PR #524 to solve this issue. Please merge it at your earliest convenience, as this is a critical bug affecting session validity.

Thank you!

Originally created by @wh5938316 on GitHub (Nov 15, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/539 @Bekacru When enabling secondary storage, the session expiration period is significantly shortened. The root cause lies in the following issue: [the code](https://github.com/better-auth/better-auth/blob/1b6c89e8d3d0c7385dce655b5dd1b43399859858/packages/better-auth/src/db/internal-adapter.ts#L237C51-L237C68). The `Date.now()` function returns the value in milliseconds, while the sessionExpiration variable is in seconds. The code incorrectly adds these two values directly without converting sessionExpiration to milliseconds using `getDate(sessionExpiration, "ms")`. This results in session expiration being 1/1000th of the intended duration when secondary storage is enabled. I have submitted a PR #524 to solve this issue. Please merge it at your earliest convenience, as this is a critical bug affecting session validity. Thank you!
GiteaMirror added the locked label 2026-04-15 14:53:50 -05:00
Author
Owner

@Bekacru commented on GitHub (Nov 15, 2024):

First, the only issue should occur is when trying to list active sessions from secondary storage. Session expiration isn't determined by the value you mentioned, it's determined by the actual expiresAt field stored in the data. The value you highlighted is intended to be used as ttl, which, as mentioned in the docs, is specified in seconds, not ms.
And thank you for the PR, but getDate() returns the current time plus the time range, it doesn't convert it to ms. A patch has already been released in the latest version, so that shouldn't be an issue anymore.

<!-- gh-comment-id:2478330191 --> @Bekacru commented on GitHub (Nov 15, 2024): First, the only issue should occur is when trying to list active sessions from secondary storage. Session expiration isn't determined by the value you mentioned, it's determined by the actual `expiresAt` field stored in the data. The value you highlighted is intended to be used as `ttl`, which, as mentioned in the docs, is specified in seconds, not ms. And thank you for the PR, but `getDate()` returns the current time plus the time range, it doesn't convert it to ms. A patch has already been released in the latest version, so that shouldn't be an issue anymore.
Author
Owner

@wh5938316 commented on GitHub (Nov 15, 2024):

@Bekacru
Apologies for not thoroughly reviewing the usage of getDate. I’m glad it’s fixed now.

Regarding secondary storage, it does seem to have quite a few issues. Could you take a look at the issue I posted about it #522?

Thanks!

<!-- gh-comment-id:2478589473 --> @wh5938316 commented on GitHub (Nov 15, 2024): @Bekacru Apologies for not thoroughly reviewing the usage of getDate. I’m glad it’s fixed now. Regarding secondary storage, it does seem to have quite a few issues. Could you take a look at the issue I posted about it #522? Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#16942