Search code examples
ruby-on-rails-3.2sidekiqsidekiq-monitor

Sidekiq with authlogic


I am using Sidekiq with Authlogic to avoid unwanted users visit. I followed everything from this link but its not working as expected. I used below code

# lib/admin_constraint.rb
class AdminConstraint
  def matches?(request)
    return false unless request.cookie_jar['user_credentials'].present?
    user = User.find_by_persistence_token(request.cookie_jar['user_credentials'].split(':')[0])
    user && user.admin?
  end
end

# config/routes.rb
require "admin_constraint"
mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new

If I logged in as Admin user its working properly. If I logged in as normal user or without logging in it shows Routing Error when i try to access that page.

No route matches [GET] "/sidekiq"

I want to clear this error. Someone please help me. Thanks in advance.

I am using rails 3.2.13 and ruby version 2.1.5


Solution

  • Try the following thing, it should work.

    class AdminConstraint
      def matches?(request)
        return false unless request.cookies['user_credentials'].present?
        user = User.find_by_persistence_token(request.cookies['user_credentials'].split(':')[0])
        user && user.admin?
      end
    end