Skip to content

Commit 8544a62

Browse files
dadachiclaude
andcommitted
Fix ActionCable find_account to use request path
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 89973d9 commit 8544a62

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

app/channels/application_cable/connection.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ def find_shopkeeper
1313
env["warden"]&.user(:shopkeeper)
1414
end
1515

16-
# Display pages are public — anonymous connections are allowed.
17-
# Shopkeeper auth is header-based (devise_token_auth), so most
18-
# WebSocket connections will be anonymous. If an authenticated-only
19-
# channel is added in the future, reject in that channel's #subscribed.
16+
# Extract the account UUID from the WebSocket upgrade request path,
17+
# matching how AccountMiddleware sets the current account for HTTP
18+
# requests. Display page URLs (display/shops/...) don't include an
19+
# account UUID, so this returns nil for public connections.
2020
def find_account
21-
current_shopkeeper&.accounts&.order(created_at: :asc)&.first
21+
_, account_id, = request.path.split("/", 3)
22+
Account.find_by(id: account_id) if AccountMiddleware::UUID_MATCHER.match?(account_id)
2223
end
2324
end
2425
end

test/channels/application_cable/connection_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@ class ApplicationCable::ConnectionTest < ActionCable::Connection::TestCase
1414
warden = Minitest::Mock.new
1515
warden.expect(:user, shopkeeper, [:shopkeeper])
1616

17-
connect env: {"warden" => warden}
17+
connect "/#{account.id}/cable", env: {"warden" => warden}
1818

1919
assert_equal shopkeeper, connection.current_shopkeeper
2020
assert_equal account, connection.current_account
2121
warden.verify
2222
end
23+
24+
test "connection without account UUID in path has nil account" do
25+
shopkeeper = shopkeepers(:one)
26+
warden = Minitest::Mock.new
27+
warden.expect(:user, shopkeeper, [:shopkeeper])
28+
29+
connect "/cable", env: {"warden" => warden}
30+
31+
assert_equal shopkeeper, connection.current_shopkeeper
32+
assert_nil connection.current_account
33+
warden.verify
34+
end
2335
end

0 commit comments

Comments
 (0)