On Rails 8's generated authentication, how do you cut a dismissed stock-room clerk off immediately, and which generated paths would still let them in?
answer
- the Session row is the authority
- user.sessions.destroy_all
- Cable checks only at connect
- remote_connections.where(...).disconnect
- a pending reset link signs back in
basics
~20 sDestroy the clerk's Session rows with user.sessions.destroy_all, disconnect their open Action Cable sockets, and block the account at sign-in and reset, because a still-valid reset email lets them set a password and sign in again.
solid answer
~40 sIn the generated code a browser is signed in only while its `Session` row exists, so `user.sessions.destroy_all` signs the clerk out everywhere on their next request; the permanent cookie then points at nothing. Three paths survive that. Open Action Cable connections were authenticated once in `ApplicationCable::Connection#connect`, so you close them with `ActionCable.server.remote_connections.where(current_user: user).disconnect`. A reset email sent within the last 15 minutes still verifies, and `PasswordsController` would let them set a new password and then sign in, so you need a deactivation flag checked in `set_user_by_token` and at sign-in, for example `User.authenticate_by(email_address:, password:, active: true)`. And nothing in the generated session expires, so a session nobody revokes lives for the cookie's 20 years. Deleting the user instead removes sessions via `dependent: :destroy`, and later reset links raise `RecordNotFound`.
code
ruby · 17 linesclass ClerkDeactivation
def initialize(clerk)
@clerk = clerk
end
def call
@clerk.update!(active: false)
@clerk.sessions.destroy_all
ActionCable.server.remote_connections
.where(current_user: @clerk).disconnect(reconnect: false)
end
end
# app/controllers/sessions_controller.rb
user = User.authenticate_by(
email_address: params[:email_address], password: params[:password], active: true
)go deeper
Know that the generated login lives in Session rows, so destroying a user's rows signs them out of every browser.
Explain why the cookie becomes a dead pointer once its row is gone, and why reset_session does not touch the generated login.
Find the surviving paths: Cable sockets authenticated once at connect, reset tokens independent of sessions, and the absence of any generated expiry, then close each one.
Choose between deactivation and deletion against audit needs, and decide whether revocation alone meets the access-removal requirement or a session time limit is owed.
## The scenario A stock-room tool built on `bin/rails generate authentication` has a clerk who has just been dismissed. The manager wants them unable to record withdrawals from this minute. The question is which generated mechanisms already do that, and which leave a door open. ## Revoking browser sessions The generated design keeps a **`Session` row per signed-in browser** and a signed `session_id` cookie that only points at it. `find_session_by_cookie` runs `Session.find_by(id: cookies.signed[:session_id])` on every request through `before_action :require_authentication`. So: - `clerk.sessions.destroy_all` revokes **every** browser at once. Each one's next request finds no row and is redirected to `new_session_path`. - `Session.find(id).destroy` revokes **one** browser, which makes a "sign out this device" page cheap to build from the stored `ip_address` and `user_agent`. - The generated `PasswordsController#update` already calls `@user.sessions.destroy_all` after a reset, for the same reason. The cookie itself cannot be recalled, but it does not need to be: without the row it is a dead pointer. ## Path 1: open Action Cable connections When Action Cable is loaded, the generator writes `ApplicationCable::Connection` with `identified_by :current_user`. It looks the session up **once**, in `connect`, and calls `reject_unauthorized_connection` if nothing matches. A socket opened before you destroyed the rows **stays open** and keeps receiving broadcasts. Close it explicitly: ```ruby ActionCable.server.remote_connections.where(current_user: clerk).disconnect(reconnect: false) ``` This broadcasts a disconnect through Action Cable's internal channel to every server. Any reconnect then runs `connect` again and is rejected. ## Path 2: a reset email already in the inbox `user.password_reset_token` is a signed message valid for 15 minutes by default (configurable with `reset_token: { expires_in: }` in Rails 8.1). Destroying sessions does **not** invalidate it; only a password change, expiry or a secret change does. A clerk who requested a reset before leaving can follow the link, set a new password, then sign in through `SessionsController#create`. Close it with an explicit flag, because the generator has none: 1. Add an `active` boolean (or a `deactivated_at` timestamp) to `users`. 2. Sign in with `User.authenticate_by(email_address: ..., password: ..., active: true)`; every non-password attribute becomes part of the `find_by`, so an inactive user is treated like an unknown one. 3. In `set_user_by_token`, treat an inactive user like an invalid token. 4. Optionally set a random password on deactivation: the new salt voids every outstanding reset link at once. ## Path 3: sessions nobody revoked | Generated piece | Lifetime | |---|---| | `session_id` cookie | `permanent`, expiring 20 years ahead | | `Session` row | no expiry column; lives until destroyed | | Reset token | 15 minutes, or until the password changes | The generated code never expires a session on its own. If the requirement is "access ends when employment ends", revocation is your only lever unless you add a time check to `find_session_by_cookie` (for example comparing the row's `created_at` or `updated_at` with a limit). How long sessions should live is a session-lifecycle policy; the Rails point is that the generator ships none. ## Deleting versus deactivating - `clerk.destroy` removes the sessions through `has_many :sessions, dependent: :destroy`. An outstanding reset link then makes `find_by_password_reset_token!` raise `ActiveRecord::RecordNotFound`, which the generated controller does not rescue, so the link ends in a 404. - Deactivating keeps the history (who recorded which withdrawal) that a stock-room audit usually needs, so it is the more common choice. It is also the choice that requires the explicit checks above. ## A deactivation checklist 1. `clerk.update!(active: false)` 2. `clerk.sessions.destroy_all` 3. `ActionCable.server.remote_connections.where(current_user: clerk).disconnect(reconnect: false)` 4. Make sure sign-in and `set_user_by_token` refuse inactive users.
- Why does the generated ApplicationCable::Connection not notice that a Session row was destroyed?It authenticates only in `connect`, via `Session.find_by(id: cookies.signed[:session_id])`, and stores the user with `identified_by :current_user`. A WebSocket is long-lived; later messages reuse that identity without another lookup. Only a disconnect followed by a fresh `connect` re-runs the check.
- Does calling reset_session in a controller help here?No. `reset_session` clears Rails' own session hash in the current request. The generated login lives in a separate signed `session_id` cookie and a `sessions` table, and it is per browser, so it neither touches the clerk's other browsers nor removes their rows.
- Deleting the clerk's cookie from the server signs them out everywhere.
- destroy_all on sessions also invalidates outstanding password reset links.
- Generated sessions expire after Devise's default 30 minutes of inactivity.
- Action Cable re-checks the Session row on every message it receives.
- reset_session signs a user out of all their browsers.