Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 415
Stef Hej Avatar asked Apr 24 '26 02:04

Stef Hej


1 Answers

As you can see in the notesof 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

like image 146
rick Avatar answered Apr 26 '26 20:04

rick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!