14

I am running a Rails 5.0.0 app with Ruby 2.3.1

Sidekiq is being used for background jobs and devise for authentication.

Sidekiq monitoring and devise are mounted in routes as follows:

devise_for :users, skip: [:sessions]
    as :user do
        get    'login' => 'devise/sessions#new',      :as => :new_user_session
        post   'login' => 'devise/sessions#create',   :as => :user_session
        delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
    end

require 'sidekiq/web'
    require 'sidekiq/cron/web'
    #Sidekiq::Web.set :session_secret, Rails.application.secrets[:secret_key_base]
    authenticate :user do
        mount Sidekiq::Web => '/sidekiq'
    end

But, accessing the sidekiq status page logs out the user.

The same code used to work fine with Rails 4.2.5

Nikhil M
  • 123
  • 1
  • 7
  • I would search through these gems github repositories. Rails 5.0 and ruby 2.3 are very recent, they may not be fully compatible with the gems. – lcguida Aug 03 '16 at 12:16
  • If an urgent fix is needed you can remove devise and roll your own auth with bcrypt since that seems to be working fine for 5.0. – bkunzi01 Aug 03 '16 at 12:21

2 Answers2

13

Try wrapping your mounting of Sidekiq under devise_scope, in the same way you're using its alias "as" in your devise_for route:

# Only allow authenticated users to get access
# to the Sidekiq web interface
devise_scope :user do
  authenticated :user do
    mount Sidekiq::Web => '/sidekiq'
  end
end
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
5

Here's a snippet for that allows for custom authentication on the Sidekiq routes.

authenticate :user, ->(user) { user.admin? || Other auth related checks... } do
  mount Sidekiq::Web => "/sidekiq"
end
ethaning
  • 386
  • 4
  • 12
  • Any documentation or examples on how to test this in rspec? – user2012677 Jun 01 '21 at 12:32
  • For those seeking such, here are the official docs around this route authentication support: - https://github.com/heartcombo/devise/wiki/How-To:-Define-resource-actions-that-require-authentication-using-routes.rb - https://github.com/heartcombo/devise/blob/ec0674523e7909579a5a008f16fb9fe0c3a71712/lib/devise/rails/routes.rb#L274-L294 – LouieGeetoo Aug 03 '23 at 18:58