Answer the question
In order to leave comments, you need to log in
Rails 4. Problem with websocket-rails and authlogic. What to do?
Problem in the following:
I do a chat. Only an authorized user can leave a message in the chat.
The controller that handles websocket requests is inherited from WebsocketRails::BaseController
class MessagesController < WebsocketRails::BaseController
def create
abort current_user
end
end
class MessagesController < WebsocketRails::BaseController
include ApplicationHelper
before_action do
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
end
def create
abort current_user
end
end
Answer the question
In order to leave comments, you need to log in
Hooray! Found a solution.
It turns out that an object inherited from WebsocketRails::BaseController, in the absence of a method, refers to a proxy object that is inherited from ApplicationController. But the cookies method is still unavailable in it.
You can get cookies inside WebsocketRails::BaseController descendants via the request.cookie method, and use them to get the current user.
Here's what happened in the end:
class MessagesController < WebsocketRails::BaseController
include ApplicationHelper
before_action do
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
end
def cookies
request.cookies
end
def create
abort current_user.id.to_s
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question