D
D
Dima2015-04-19 21:05:52
Ruby on Rails
Dima, 2015-04-19 21:05:52

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

The problem is that the current_user method is missing, and you need to know who the message is from.
I connect the helper from the ApplicationController, in which this method is defined (I use Authlogic for authentication)
Another error occurs - "You must activate the Authlogic::Session::Base.controller with a controller object before creating objects" - throws out Authlogic.
OK.
I write the following in the MessagesController controller:
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

All is well, but it turns out that there is no cookies method in the Messages controller (NoMethodError: undefined method `cookies'). And rightly so, because it does not inherit from ActionController::Base but from WebsocketRails::BaseController.
Question: Has anyone used websocket-rails + authlogic? If yes, then throw off the link to Github, where you can see the code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima, 2015-04-20
@MAXOPKA

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 question

Ask a Question

731 491 924 answers to any question