[GH-ISSUE #1087] lastSignIn field for users #8588

Closed
opened 2026-04-13 03:42:23 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @daveycodez on GitHub (Dec 31, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1087

Is this suited for github?

  • Yes, this is suited for github

No response

Describe the solution you'd like

It would be great to have an easy way to figure out when a user was last logged in, and doesn't depend on sessions since those expire and can be cleaned up

Describe alternatives you've considered

Manually update user row whenever a session is created

Additional context

No response

Originally created by @daveycodez on GitHub (Dec 31, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1087 ### Is this suited for github? - [X] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. _No response_ ### Describe the solution you'd like It would be great to have an easy way to figure out when a user was last logged in, and doesn't depend on sessions since those expire and can be cleaned up ### Describe alternatives you've considered Manually update user row whenever a session is created ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 03:42:23 -05:00
Author
Owner

@daveycodez commented on GitHub (Dec 31, 2024):

lastSignIn or lastActive would be great. Would be especially helpful when determining when to clean out anonymous users (based on last sign in)

<!-- gh-comment-id:2566717299 --> @daveycodez commented on GitHub (Dec 31, 2024): lastSignIn or lastActive would be great. Would be especially helpful when determining when to clean out anonymous users (based on last sign in)
Author
Owner

@Bekacru commented on GitHub (Jan 3, 2025):

You can easily achieve this by adding a lastLogin field to the user table and using hooks to update it. And we could potentially make this a plugin, but adding it by default may not be the best idea.

<!-- gh-comment-id:2568763852 --> @Bekacru commented on GitHub (Jan 3, 2025): You can easily achieve this by adding a `lastLogin` field to the user table and using hooks to update it. And we could potentially make this a plugin, but adding it by default may not be the best idea.
Author
Owner

@daveycodez commented on GitHub (Jan 3, 2025):

What is the best hook to add this to?

I see there are multiple ways

https://www.better-auth.com/docs/concepts/hooks

https://www.better-auth.com/docs/concepts/database#database-hooks

I feel like this hook would be on get-session? And trigger an update to the user? So not a database hook?

<!-- gh-comment-id:2568941942 --> @daveycodez commented on GitHub (Jan 3, 2025): What is the best hook to add this to? I see there are multiple ways https://www.better-auth.com/docs/concepts/hooks https://www.better-auth.com/docs/concepts/database#database-hooks I feel like this hook would be on get-session? And trigger an update to the user? So not a database hook?
Author
Owner

@Bekacru commented on GitHub (Jan 3, 2025):

assuming this is when the user last logged in, you can use db hooks after a session is created or you can also use hooks and check if there is a new session from ctx.context.newSession after specefic endpoints.

<!-- gh-comment-id:2569014150 --> @Bekacru commented on GitHub (Jan 3, 2025): assuming this is when the user last logged in, you can use db hooks after a session is created or you can also use hooks and check if there is a new session from `ctx.context.newSession` after specefic endpoints.
Author
Owner

@Bekacru commented on GitHub (Jan 3, 2025):

If this is to when they user is last active, that should be more of a client initated action. Use a web analytics or put a tracker that sends a request just before the user session is closed.

<!-- gh-comment-id:2569015279 --> @Bekacru commented on GitHub (Jan 3, 2025): If this is to when they user is last active, that should be more of a client initated action. Use a web analytics or put a tracker that sends a request just before the user session is closed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8588