Skip to content

Commit 89973d9

Browse files
dadachiclaude
andcommitted
Add ActionCable connection identification
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7c65d61 commit 89973d9

4 files changed

Lines changed: 47 additions & 7 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
module ApplicationCable
22
class Channel < ActionCable::Channel::Base
3+
# All current channels are public (Turbo::StreamsChannel for display pages).
4+
# If an authenticated channel is added in the future, reject unauthorized
5+
# connections in that channel's #subscribed method:
6+
#
7+
# def subscribed
8+
# reject unless connection.current_shopkeeper
9+
# end
310
end
411
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
module ApplicationCable
22
class Connection < ActionCable::Connection::Base
3+
identified_by :current_shopkeeper, :current_account
4+
5+
def connect
6+
self.current_shopkeeper = find_shopkeeper
7+
self.current_account = find_account
8+
end
9+
10+
private
11+
12+
def find_shopkeeper
13+
env["warden"]&.user(:shopkeeper)
14+
end
15+
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.
20+
def find_account
21+
current_shopkeeper&.accounts&.order(created_at: :asc)&.first
22+
end
323
end
424
end

app/views/display/shops/show.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<%# These streams are public by design — display pages are unauthenticated %>
12
<%= turbo_stream_from @shop, :tb_stream_full_reload_entire_page %>
23
<%= turbo_stream_from @shop, :tb_stream_update_item_tags %>
34

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
require "test_helper"
22

33
class ApplicationCable::ConnectionTest < ActionCable::Connection::TestCase
4-
# test "connects with cookies" do
5-
# cookies.signed[:user_id] = 42
6-
#
7-
# connect
8-
#
9-
# assert_equal connection.user_id, "42"
10-
# end
4+
test "anonymous connection succeeds with nil shopkeeper and account" do
5+
connect
6+
7+
assert_nil connection.current_shopkeeper
8+
assert_nil connection.current_account
9+
end
10+
11+
test "authenticated connection identifies shopkeeper and account" do
12+
shopkeeper = shopkeepers(:one)
13+
account = shopkeeper.create_default_account
14+
warden = Minitest::Mock.new
15+
warden.expect(:user, shopkeeper, [:shopkeeper])
16+
17+
connect env: {"warden" => warden}
18+
19+
assert_equal shopkeeper, connection.current_shopkeeper
20+
assert_equal account, connection.current_account
21+
warden.verify
22+
end
1123
end

0 commit comments

Comments
 (0)