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

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

我该用什么?

  • Rails 5.0.1 –api (我没有任何意见NOR使用咖啡)
  • 我使用react-native app来测试这个(没有授权就可以正常工作)
  • 我没有使用devise for auth (我使用JWT而不是使用Knock ,所以没有cookie)

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

代码看起来像

 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 

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

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

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

 [NameError - undefined local variable or method `current_user' for #] 

如果您看到您提供的文档,您将知道identified_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 

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

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

如果您在rails中使用设计gem,请替换此function:

 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 

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