LoginSignup
1

More than 5 years have passed since last update.

Action Cableとdeviseの併用

Posted at
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_session_user

    def connect
      self.current_session_user = find_session_user
    end

    protected

    def find_session_user
      session_user = env['warden'].user(scope: :operator)
      return reject_unauthorized_connection unless session_user
      session_user
    end
  end
end

認証ユーザーのscopeが1つなら env['warden'].user だけで問題ないが、
複数あるなら scopeの指定が必要.

 env['warden'].user(scope: :operator)

devise helperの current_user のようにChannelで参照できる
Controllerのhelperではないので、devise helperの current_user と競合しないため、
current_userと定義していても問題はない

identified_by :current_session_user

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1