fix: disconnect a disabled user's open LiveViews #54

Merged
jfro merged 2 commits from jtk/disconnect-disabled-users into main 2026-10-18 22:39:39 +00:00
Owner

Disabling a user deleted their session tokens, but LiveViews they already had open stayed connected, because log_in_user never put :live_socket_id in the session. With nothing in the session, no disconnect broadcast could ever reach them.

Change

  • UserAuth.put_token_in_session/2 now also sets :live_socket_id to "users_sessions:#{Base.url_encode64(token)}", the topic Phoenix's socket listens on. This covers every login path (password, magic link, OIDC, session reissue), since they all go through create_or_extend_session.
  • New UserAuth.disconnect_sessions/1 takes the deleted UserTokens, broadcasts "disconnect" for each session token, and logs how many it disconnected and for which user ID.
  • Accounts.update_user_by_admin/2 now returns {:ok, {user, expired_tokens}}, the same shape as update_user_password/2. Disabling returns the deleted tokens; other changes return []. AdminUsersLive passes them to disconnect_sessions/1. The broadcast lives in the web layer, so Accounts doesn't call the endpoint.
  • The same applies on password change: the settings controller now disconnects LiveViews still open on the old sessions (other devices). It's the same mechanism and one line; tell me if you'd rather it went in a separate PR.
  • log_out_user/1 already broadcast on :live_socket_id, so logging out now disconnects too.

Tests

  • log_in_user stores the right :live_socket_id.
  • disconnect_sessions/1 broadcasts on each session topic and skips non-session tokens.
  • AdminUsersLive: disabling a user broadcasts disconnect on their topic; changing their role doesn't. This test failed before the fix.
  • Settings: a password change broadcasts disconnect for the old session.
  • Updated the fixture and tests for the new update_user_by_admin return shape.

mix precommit passes: 542 tests, 0 failures.

Fixes #36

Note for release: sessions created before this deploys have no :live_socket_id, so their open LiveViews won't get the disconnect until the user logs in again or the session token is reissued.

Disabling a user deleted their session tokens, but LiveViews they already had open stayed connected, because `log_in_user` never put `:live_socket_id` in the session. With nothing in the session, no disconnect broadcast could ever reach them. **Change** - `UserAuth.put_token_in_session/2` now also sets `:live_socket_id` to `"users_sessions:#{Base.url_encode64(token)}"`, the topic Phoenix's socket listens on. This covers every login path (password, magic link, OIDC, session reissue), since they all go through `create_or_extend_session`. - New `UserAuth.disconnect_sessions/1` takes the deleted `UserToken`s, broadcasts `"disconnect"` for each session token, and logs how many it disconnected and for which user ID. - `Accounts.update_user_by_admin/2` now returns `{:ok, {user, expired_tokens}}`, the same shape as `update_user_password/2`. Disabling returns the deleted tokens; other changes return `[]`. `AdminUsersLive` passes them to `disconnect_sessions/1`. The broadcast lives in the web layer, so `Accounts` doesn't call the endpoint. - The same applies on password change: the settings controller now disconnects LiveViews still open on the old sessions (other devices). It's the same mechanism and one line; tell me if you'd rather it went in a separate PR. - `log_out_user/1` already broadcast on `:live_socket_id`, so logging out now disconnects too. **Tests** - `log_in_user` stores the right `:live_socket_id`. - `disconnect_sessions/1` broadcasts on each session topic and skips non-session tokens. - `AdminUsersLive`: disabling a user broadcasts disconnect on their topic; changing their role doesn't. This test failed before the fix. - Settings: a password change broadcasts disconnect for the old session. - Updated the fixture and tests for the new `update_user_by_admin` return shape. `mix precommit` passes: 542 tests, 0 failures. Fixes #36 **Note for release:** sessions created before this deploys have no `:live_socket_id`, so their open LiveViews won't get the disconnect until the user logs in again or the session token is reissued.
jfro added this to the 0.6.2 milestone 2026-10-18 21:42:59 +00:00
fix: disconnect a disabled user's open LiveViews
All checks were successful
CI / test (pull_request) Successful in 1m45s
e4ca5e78eb
log_in_user never put :live_socket_id in the session, so nothing could
broadcast a disconnect to a user's LiveViews. Store it next to the
session token, add UserAuth.disconnect_sessions/1, and call it with the
deleted tokens when an admin disables a user (and on password change).
update_user_by_admin/2 now returns {user, expired_tokens}, like
update_user_password/2.

Fixes #36
Author
Owner

Review from Nye (approved)

Checked: topic matches what Phoenix.LiveView.Socket reads from session "live_socket_id" and what log_out_user already broadcasts on; every login path goes through create_or_extend_session; reissue leaves the old token in the DB so disable still reaches the old topic; disabled-user mount fails since verify_session_token_query requires status active; no update_user_by_admin callers left on the old shape. Password-change disconnect in this PR is fine.

Nits (non-blocking):

  1. Sessions created before deploy have no :live_socket_id, so their open LiveViews won't get the disconnect until the user logs in again or the token reissues. Worth a line in the PR body / release notes.
  2. UserSessionController magic-link create ignores _expired_tokens from login_user_by_magic_link (the confirm path deletes all tokens). phx.gen.auth passes these to disconnect_sessions; call it there too for parity.
  3. The disconnect log could include the acting admin's id for audit.
Review from Nye (approved) Checked: topic matches what Phoenix.LiveView.Socket reads from session "live_socket_id" and what log_out_user already broadcasts on; every login path goes through create_or_extend_session; reissue leaves the old token in the DB so disable still reaches the old topic; disabled-user mount fails since verify_session_token_query requires status active; no update_user_by_admin callers left on the old shape. Password-change disconnect in this PR is fine. Nits (non-blocking): 1. Sessions created before deploy have no :live_socket_id, so their open LiveViews won't get the disconnect until the user logs in again or the token reissues. Worth a line in the PR body / release notes. 2. UserSessionController magic-link create ignores _expired_tokens from login_user_by_magic_link (the confirm path deletes all tokens). phx.gen.auth passes these to disconnect_sessions; call it there too for parity. 3. The disconnect log could include the acting admin's id for audit.
fix: disconnect sessions expired by magic link confirmation
All checks were successful
CI / test (pull_request) Successful in 1m56s
7c0c8bd7b2
Also log the acting admin when an admin change disconnects a user.
jfro merged commit 73a62c1552 into main 2026-10-18 22:39:39 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/fuzzy_catalog!54
No description provided.