Skip to content

Add ability to view and terminate user sessions - #3664

Merged
hughrun merged 7 commits into
bookwyrm-social:mainfrom
hughrun:sessions
Aug 23, 2025
Merged

hughrun merged 7 commits into
bookwyrm-social:mainfrom
hughrun:sessions

Conversation

@hughrun

@hughrun hughrun commented Aug 11, 2025

Copy link
Copy Markdown
Member

Adds a dashboard in user preferences to enable users to see all logged-in sessions under their account, and terminate any if they choose to do so.

Also renames 2FA page to "Security Settings" since it seemed reasonable to put these two functionalities together.

Because we use the cache engine for sessions, we can't efficiently check directly for all a user's sessions. Instead we add an object to a new UserSession model the first time they log in to a session. On logout, the actual session is terminated, and we delete the model object. Because sessions can expire, we check all associated sessions for a user's UserSession objects when they load the security page and remove any that no longer have active sessions.

Existing sessions when this is first rolled out will not appear in the dashboard since they won't have a UserSession object. Once they expire, this problem will resolve itself.

Users can't accidentally log out their current session ;)

This also changes the default session age to 365 days.

light_sessions dark_sessions

What type of Pull Request is this?

  • Bug Fix
  • Enhancement
  • Plumbing / Internals / Dependencies
  • Refactor

Does this PR change settings or dependencies, or break something?

  • This PR changes or adds default settings, configuration, or .env values
  • This PR changes or adds dependencies
  • This PR introduces other breaking changes

Details of breaking or configuration changes (if any of above checked)

Documentation

  • New or amended documentation will be required if this PR is merged
  • I have created a matching pull request in the Documentation repository
  • I intend to create a matching pull request in the Documentation repository after this PR is merged

Tests

  • My changes do not need new tests
  • All tests I have added are passing
  • I have written tests but need help to make them pass
  • I have not written tests and need help to write them

Adds a dashboard in user preferences to enable users to see all logged-in sessions under their account, and terminate any if they choose to do so.

Also renames 2FA page to "Security Settings" since it seemed reasonable to put these two functionalities together.

Because we use the cache engine for sessions, we can't efficiently check directly for all a user's sessions. Instead we add an object to a new UserSession model the first time they log in to a session. On logout, the actual session is terminated, and we delete the model object. Because sessions can expire, we check all associated sessions for a user's UserSession objects when they load the security page and remove any that no longer have active sessions.

Existing sessions when this is first rolled out will not appear in the dashboard since they won't have a UserSession object. Once they expire, this problem will resolve itself.
@hughrun
hughrun requested review from dato and mouse-reeve and removed request for dato August 11, 2025 07:01
Comment thread bookwyrm/models/session.py Resolved

@dato dato left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! πŸŽ‰

I ran this on my test instance, it worked as documented.

Please find some review comments below.

Comment thread bookwyrm/models/session.py Resolved
Comment thread bookwyrm/models/user.py Resolved
Comment thread bookwyrm/models/session.py Resolved
Comment thread bookwyrm/models/session.py Resolved
Comment thread bookwyrm/tests/models/test_session.py Resolved
@hughrun hughrun added the enhancement New feature or request label Aug 22, 2025
hughrun and others added 3 commits August 23, 2025 14:24
fix abbr

Co-authored-by: Adeodato SimΓ³ <73768+dato@users.noreply.github.com>
rationalise ua_parser usage

Co-authored-by: Adeodato SimΓ³ <73768+dato@users.noreply.github.com>
@hughrun
hughrun requested a review from dato August 23, 2025 05:35

@dato dato left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@hughrun
hughrun removed the request for review from mouse-reeve August 23, 2025 22:01
@hughrun
hughrun merged commit 335b32c into bookwyrm-social:main Aug 23, 2025
@hughrun
hughrun deleted the sessions branch October 11, 2025 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make sessions last longer Add an option to view and terminate activity sessions

3 participants