如何在ActionCable rails-5-api app中获取current_user?

时间:2021-08-17 01:14:26

Why am I not able to retrieve current_user inside my channel or how should I retrieve current_user?

为什么我无法在通道中检索current_user或如何检索current_user?

What do I use?

我该用什么?

  • Rails 5.0.1 --api (I do NOT have any views NOR use coffee)
  • Rails 5.0.1 --api(我没有任何意见NOR使用咖啡)

  • I use react-native app to test this (Works fine WITHOUT authorization)
  • 我使用react-native app来测试这个(没有授权就可以正常工作)

  • I do NOT use devise for auth (I use JWT instead using Knock, so no cookies)
  • 我没有使用devise for auth(我使用JWT而不是使用Knock,所以没有cookie)

Trying to get current_user inside my ActionCable channel as described in rubydoc.info

尝试在rubydoc.info中描述的ActionCable通道中获取current_user

The code looks like

代码看起来像

class MessageChannel < ApplicationCable::Channel
  identified_by :current_user

  def subscribed
    stream_from 'message_' + find_current_user_privileges
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

  protected

  def find_current_user_privileges
    if current_user.has_role? :admin
      'admin'
    else
      'user_' + current_user.id
    end
  end

end

And running it, I get this error:

运行它,我得到这个错误:

[NoMethodError - undefined method `identified_by' for MessageChannel:Class]

And if I remove identified_by :current_user, I get

如果我删除identify_by:current_user,我会得到

[NameError - undefined local variable or method `current_user' for #<MessageChannel:0x7ace398>]

2 个解决方案

#1


3  

If you see the doc you provided, you will know that identified_by is not a method for a Channel instance. It is a method for Actioncable::Connection. From Rails guide for Actioncable Overview, this is how a Connection class looks like:

如果您看到您提供的文档,您将知道identify_by不是Channel实例的方法。它是Actioncable :: Connection的一种方法。从Actioncable Overview的Rails指南中,这是Connection类的外观:

#app/channels/application_cable/connection.rb
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    private
      def find_verified_user
        if current_user = User.find_by(id: cookies.signed[:user_id])
          current_user
        else
          reject_unauthorized_connection
        end
      end
  end
end

As you can see, current_user is not available here. Instead, you have to create a current_user here in connection.

如您所见,current_user在此处不可用。相反,您必须在此处创建current_user。

The websocket server doesn't have a session, but it can read the same cookies as the main app. So I guess, you need to save cookie after authentication.

websocket服务器没有会话,但它可以读取与主应用程序相同的cookie。所以我想,你需要在认证后保存cookie。

#2


0  

if u are using devise gems in rails, Please replace this function:

如果您在rails中使用设计宝石,请替换此功能:

def find_verified_user # this checks whether a user is authenticated with devise
  if verified_user = env['warden'].user
    verified_user
  else
    reject_unauthorized_connection
  end
end

I hope this will help u.

我希望这会对你有所帮助。

#1


3  

If you see the doc you provided, you will know that identified_by is not a method for a Channel instance. It is a method for Actioncable::Connection. From Rails guide for Actioncable Overview, this is how a Connection class looks like:

如果您看到您提供的文档,您将知道identify_by不是Channel实例的方法。它是Actioncable :: Connection的一种方法。从Actioncable Overview的Rails指南中,这是Connection类的外观:

#app/channels/application_cable/connection.rb
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    private
      def find_verified_user
        if current_user = User.find_by(id: cookies.signed[:user_id])
          current_user
        else
          reject_unauthorized_connection
        end
      end
  end
end

As you can see, current_user is not available here. Instead, you have to create a current_user here in connection.

如您所见,current_user在此处不可用。相反,您必须在此处创建current_user。

The websocket server doesn't have a session, but it can read the same cookies as the main app. So I guess, you need to save cookie after authentication.

websocket服务器没有会话,但它可以读取与主应用程序相同的cookie。所以我想,你需要在认证后保存cookie。

#2


0  

if u are using devise gems in rails, Please replace this function:

如果您在rails中使用设计宝石,请替换此功能:

def find_verified_user # this checks whether a user is authenticated with devise
  if verified_user = env['warden'].user
    verified_user
  else
    reject_unauthorized_connection
  end
end

I hope this will help u.

我希望这会对你有所帮助。