devise: Warden Key Missing - Action Cable

I’ve got an up to date Rails 5 app running Devise, and Action Cable. I can authorize the connection just fine, that is not the problem. The issue is using current_user helper within a notification partial that’s pushed out using Action Cable. It’s causing an error because no middleware is exec in Action Cable.

TLDR;

  • model record contains an after action that creates a notification record
  • notifications are background jobs, thus they do NOT stem from a controller
  • current_user helper causing error of missing warden key when used in a partial broadcasted with action cable

This sums it up better than I can. Here’s the accompanying link: https://evilmartians.com/chronicles/new-feature-in-rails-5-render-views-outside-of-actions

image 2017-03-13 at 9 33 55 am

I’ve tried a few work arounds and haven’t had much luck, and I’m definitely not in the mindset of monkey patching. Any thoughts?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

I’m bumping this again after researching on Google how to fix this error, and coming across my own bug report from back in March. I realize you don’t get paid to maintain this gem but if you have any thoughts, I’d love to hear them.

I was facing the same problem, when I tried to use ApplicationController.render to render the partial view that I want to pass to the action cable.

I’ve got the solution from the article linked below, but this solution makes sign_in_count increase whenever rendering a partial view using ApplicationController.render_with_signed_in_user.

 def self.render_with_signed_in_user(user, *args)
   ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'
   proxy = Warden::Proxy.new({}, Warden::Manager.new({})).tap{|i| i.set_user(user, scope: :user) }
   renderer = self.renderer.new('warden' => proxy)
   renderer.render(*args)
 end

https://www.stefanwienert.de/blog/2016/04/05/using-rails-5-new-renderer-with-authentication-gems-like-clearance-or-devise/

Hi there,

I’m running a project that uses:

  • Ruby 2.3.0
  • Rails 5.0.1
  • Actioncable 5.0.6

We are using Action Cable, and for authentication using Devise and Warden, we have the following code in the app/channels/connection.rb file.

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  identified_by :current_user

 def connect
    self.current_user = find_verified_user
    logger.add_tags 'ActionCable', current_user.email
  end

  protected

  def find_verified_user
    if verified_user == env['warden'].user
     verified_user
    else
      reject_unauthorized_connection
    end
  end
end

Hope it helps. Regards.

Actually, this is an issue that happens when you want to render a partial outside of your actions…Like in a job, and your partial uses devise helpers (current_user, user_signed_in? etc). This is similar to the following issue: https://github.com/plataformatec/devise/issues/4271