简体   繁体   中英

Rails5 ActionCable include current_user method from SessionsHelper

I want to get access to my current_user method which is defined in the SessionsHelper . How can I include or require it for the ApplicationCable Connection class?

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      if current_user
        self.current_user = current_user  
        logger.add_tags current_user.name
      end
    end
  end
end

gives me

There was an exception - NoMethodError(undefined method `name' for nil:NilClass)

Equivalent to the ApplicationController I included the SessionHelper in channels/application_cable/connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    include SessionsHelper
    identified_by :current_user
    # (..)
  end
end

but doesn't work.

As you can see in the notes of the official documentation the cable can not access the session. In this article, cited in the notes, you can find a way to use cookies for managing athentication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM