|
| 1 | +--- |
| 2 | +description: Practice safe optimism |
| 3 | +--- |
| 4 | + |
| 5 | +# Authentication |
| 6 | + |
| 7 | +If you're just trying to bootstrap a proof-of-concept application on your local workstation, you don't technically have to worry about giving ActionCable the ability to distinguish between multiple concurrent users. However, **the moment you deploy to a host with more than one person accessing your app, you'll find that you're sharing a session and seeing other people's updates**. That isn't what most developers have in mind. |
| 8 | + |
| 9 | +## Encrypted Session Cookies |
| 10 | + |
| 11 | +You can use your default Rails encrypted cookie-based sessions to isolate your users into their own sessions. This works great even if your application doesn't have a login system. |
| 12 | + |
| 13 | +{% code title="app/controllers/application\_controller.rb" %} |
| 14 | +```ruby |
| 15 | +class ApplicationController < ActionController::Base |
| 16 | + before_action :set_action_cable_identifier |
| 17 | + |
| 18 | + private |
| 19 | + |
| 20 | + def set_action_cable_identifier |
| 21 | + cookies.encrypted[:session_id] = session.id.to_s |
| 22 | + end |
| 23 | +end |
| 24 | +``` |
| 25 | +{% endcode %} |
| 26 | + |
| 27 | +{% code title="app/channels/application\_cable/connection.rb" %} |
| 28 | +```ruby |
| 29 | +module ApplicationCable |
| 30 | + class Connection < ActionCable::Connection::Base |
| 31 | + identified_by :session_id |
| 32 | + |
| 33 | + def connect |
| 34 | + self.session_id = cookies.encrypted[:session_id] |
| 35 | + end |
| 36 | + end |
| 37 | +end |
| 38 | +``` |
| 39 | +{% endcode %} |
| 40 | + |
| 41 | +We need to instruct ActionCable to stream updates on a per-session basis: |
| 42 | + |
| 43 | +{% code title="app/channels/optimism_channel.rb" %} |
| 44 | +```ruby |
| 45 | +class OptimismChannel < ApplicationCable::Channel |
| 46 | + def subscribed |
| 47 | + stream_for session_id |
| 48 | + end |
| 49 | +end |
| 50 | +``` |
| 51 | +{% endcode %} |
| 52 | + |
| 53 | +Finally, we need to give Optimism the ability to broadcast updates to the correct channel subscription. We will override Optimism's default "OptimismChannel" with a lambda that returns the subscription identifier. You might already have other values in your initializer, or it might not yet exist at all: |
| 54 | + |
| 55 | +{% code title="config/initializers/optimism.rb" %} |
| 56 | +```ruby |
| 57 | +Optimism.configure do |config| |
| 58 | + config.channel = ->(context) { OptimismChannel.broadcasting_for(context.session.id) } |
| 59 | +end |
| 60 | +``` |
| 61 | +{% endcode %} |
| 62 | + |
| 63 | +## User-based Authentication |
| 64 | + |
| 65 | +Many Rails apps use the current\_user convention or more recently, the [Current](https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html) object to provide a global user context. This gives access to the user scope from _almost_ all parts of your application. |
| 66 | + |
| 67 | +{% code title="app/controllers/application\_controller.rb " %} |
| 68 | +```ruby |
| 69 | +class ApplicationController < ActionController::Base |
| 70 | + before_action :set_action_cable_identifier |
| 71 | + |
| 72 | + private |
| 73 | + |
| 74 | + def set_action_cable_identifier |
| 75 | + cookies.encrypted[:user_id] = current_user&.id |
| 76 | + end |
| 77 | +end |
| 78 | +``` |
| 79 | +{% endcode %} |
| 80 | + |
| 81 | +{% code title="app/channels/application\_cable/connection.rb " %} |
| 82 | +```ruby |
| 83 | +module ApplicationCable |
| 84 | + class Connection < ActionCable::Connection::Base |
| 85 | + identified_by :current_user |
| 86 | + |
| 87 | + def connect |
| 88 | + user_id = cookies.encrypted[:user_id] |
| 89 | + return reject_unauthorized_connection if user_id.nil? |
| 90 | + user = User.find_by(id: user_id) |
| 91 | + return reject_unauthorized_connection if user.nil? |
| 92 | + self.current_user = user |
| 93 | + end |
| 94 | + end |
| 95 | +end |
| 96 | +``` |
| 97 | +{% endcode %} |
| 98 | + |
| 99 | +We need to instruct ActionCable to stream updates on a per-session basis: |
| 100 | + |
| 101 | +{% code title="app/channels/optimism_channel.rb" %} |
| 102 | +```ruby |
| 103 | +class OptimismChannel < ApplicationCable::Channel |
| 104 | + def subscribed |
| 105 | + stream_for current_user |
| 106 | + end |
| 107 | +end |
| 108 | +``` |
| 109 | +{% endcode %} |
| 110 | + |
| 111 | +Finally, we need to give Optimism the ability to broadcast updates to the correct channel subscription. We will override Optimism's default "OptimismChannel" with a lambda that returns the subscription identifier. You might already have other values in your initializer, or it might not yet exist at all: |
| 112 | + |
| 113 | +{% code title="config/initializers/optimism.rb" %} |
| 114 | +```ruby |
| 115 | +Optimism.configure do |config| |
| 116 | + config.channel = ->(context) { OptimismChannel.broadcasting_for(context.current_user) } |
| 117 | +end |
| 118 | +``` |
| 119 | +{% endcode %} |
| 120 | + |
| 121 | +## Devise-based Authentication |
| 122 | + |
| 123 | +If you're using the versatile [Devise](https://github.com/plataformatec/devise) authentication library, your configuration is *identical* to the User-Based Authentication above, **except** for how ActionCable looks up a user: |
| 124 | + |
| 125 | +{% code title="app/channels/application\_cable/connection.rb" %} |
| 126 | +```ruby |
| 127 | +module ApplicationCable |
| 128 | + class Connection < ActionCable::Connection::Base |
| 129 | + identified_by :current_user |
| 130 | + |
| 131 | + def connect |
| 132 | + self.current_user = find_verified_user |
| 133 | + end |
| 134 | + |
| 135 | + protected |
| 136 | + |
| 137 | + def find_verified_user |
| 138 | + if current_user = env["warden"].user |
| 139 | + current_user |
| 140 | + else |
| 141 | + reject_unauthorized_connection |
| 142 | + end |
| 143 | + end |
| 144 | + end |
| 145 | +end |
| 146 | +``` |
| 147 | +{% endcode %} |
| 148 | + |
| 149 | + |
0 commit comments