Security

How to secure your StimulusReflex app

GitHub starsarrow-up-right GitHub forksarrow-up-right Twitter followarrow-up-right

StimulusReflex leans on ActionCable for securityarrow-up-right, but here's a TLDR to get you going.

circle-info

This should work with authentication solutions like Devisearrow-up-right.

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :set_action_cable_identifier

  private

  def set_action_cable_identifier
    cookies.encrypted[:user_id] = current_user&.id
  end
end
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      user_id = cookies.encrypted[:user_id]
      return reject_unauthorized_connection if user_id.nil?
      user = User.find_by(id: user_id)
      return reject_unauthorized_connection if user.nil?
      self.current_user = user
    end
  end
end

Last updated