0

I'm building an app using Rails & ActionCable, but I keep getting

undefined local variable or method `current_user'

and connect method never hits

class OrdersChannel < ApplicationCable::Channel
  def subscribed
    stream_from "market_orders_channel_#{current_user.id}"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

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

    rescue_from ActiveRecord::RecordNotFound do
      reject_unauthorized_connection
    end

    def connect
      self.current_user = find_verified_user
    end

    protected

    def find_verified_user
      token = Doorkeeper::AccessToken.find_by(token: request.params[:access_token])
      reject_unauthorized_connection if token.blank?

      Spree::User.find(token.resource_owner_id)
    end
  end
end

When I add a breakpoint in connection it is never called, and I keep getting error in subscribed method. How to solve this issue? I'm using ruby 2.5.1 & rails 5.2.0

I've tried all other solutions but none of them works

Vasilisa
  • 4,604
  • 3
  • 20
  • 25
Ahmed Ali
  • 2,574
  • 2
  • 23
  • 38
  • Possible duplicate of [How do I get current\_user in ActionCable rails-5-api app?](https://stackoverflow.com/questions/42451889/how-do-i-get-current-user-in-actioncable-rails-5-api-app) – Antarr Byrd Mar 17 '19 at 20:02

1 Answers1

0

I've figured out the issue, i wan defining action cable channels in my Spree extension not on my main project, when i moved my code to main project it works

Ahmed Ali
  • 2,574
  • 2
  • 23
  • 38