Why am I not able to retrieve current_user
inside my channel or how should I retrieve current_user
?
What do I use?
- Rails 5.0.1 --api (I do NOT have any views NOR use coffee)
- I use react-native app to test this (Works fine WITHOUT authorization)
- I do NOT use devise for auth (I use JWT instead using Knock, so no cookies)
Trying to get current_user
inside my ActionCable channel as described in rubydoc.info
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
[NameError - undefined local variable or method `current_user' for #<MessageChannel:0x7ace398>]