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
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
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
Related
I have a Rails 7 app using devise and Sidekiq for emails. Only an admin is allowed to access the Sidekiq dashboard.
Here is an excerpt from my routes file.
root to: "scores#new"
devise_for :members, controllers: {registrations: 'members/registrations',
sessions: 'members/sessions',
passwords: 'members/passwords'}
devise_scope :member do
authenticated :member do
root :to => 'scores#new', as: :authenticated_root
end
unauthenticated :member do
root :to => 'members/registrations#new', as: :unauthenticated_root
end
end
require 'sidekiq/web'
authenticate :member, ->(member) { member.user.admin? } do
mount Sidekiq::Web => '/sidekiq'
end
As soon as I sign up (recognised as an admin), it roots me to the Sidekiq dashboard. Then if I sign up as a non-admin, it still "tries" to root me to the Sidekiq dashboard and fails.
Any help is welcome.
So I've been trying to set my routes for devise as follows:
scope '(:locale)' do
devise_for :users, skip: [:registrations]
resources :questions
end
However when I authenticate in my controller, when you're not signed in devise redirects you to localhost:3000/users/sign_in.
What I expect to happen would it to redirect me to localhost:3000/en/users/sign_in. What am I doing wrong or what way is there around this?
So it turns out you can't set the routes in your scope, but rather need to set them manually. At least that's what's worked for me.
devise_for :users, skip: [:sessions]
as :user do
get ':locale/login' => 'devise/sessions#new', :as => :new_user_session
post ':locale/login' => 'devise/sessions#create', :as => :user_session
delete ':locale/logout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
# your normal scope without devise_for in it
scope '(:locale)' do
resources :questions
end
After this the redirects work as expected.
I am getting the following error when i go to root after logging in
Could not find devise mapping for path "/".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
Routes File
Rails.application.routes.draw do
root 'pages#home'
devise_for :users, controllers: { sessions: "users/sessions", registrations: "users/registrations", confirmations: "users/confirmations", passwords: "users/passwords" }, :skip => [:sessions]
as :user do
get 'sign_in' => 'users/sessions#new', :as => :new_user_session
post 'sign_in' => 'users/sessions#create', :as => :user_session
match 'sign_out' => 'users/sessions#destroy', :as => :destroy_user_session,
:via => Devise.mappings[:user].sign_out_via
end
end
Even though i have a route_path it throws up the error.
Try writing routes in scope block.
devise_scope :user do
# write all your routes inside this block
end
You can find more about scope here
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
Could not find devise mapping for path "/". This may happen for two reasons: 1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
I am getting this error when I try to go here
http://localhost:3000/
And here are the routes
devise_scope :user do
get '/users/sign_in' => 'devise/sessions#new', as: :new_user_session
post '/users/sign_in' => 'devise/sessions#create', as: :user_session
delete '/users/sign_out' => 'devise/sessions#destroy', as: :destroy_user_session
post '/users/password' => 'devise/passwords#create', as: :user_password
put '/users/password' => 'devise/passwords#update', as: nil
patch '/users/password' => 'devise/passwords#update', as: nil
authenticated :user do
root :to => 'home#index', as: :authenticated_root
end
unauthenticated :user do
root :to => 'devise/sessions#new', as: :unauthenticated_root
end
end
This set up was working earlier so I don't know why it is suddenly failing. Details about my setup (from memory): I did
bundle install
with devise as a gem
rails g devise user
rails generate devise:views
I added this line in development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Also, I am using rails 4.2.0.beta1 and devise 3.3.0 pulling from (git => 'https://github.com/plataformatec/devise.git', :branch => 'lm-rails-4-2')
Try writing routes in scope block.
devise_scope :user do
# write all your routes inside this block
end
You can find more about scope here
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
I am using Rails 4.0.2 and Devise 3.2.2 to handle user registration / authentication.
I have googled and search stackoverflow for answers, can't really find something that can answer my question.
The below code is my routes.rb, I have skip all sessions routes and registration routes but for some reason, Devise is not using my custom registrations_controller.rb because if it is, it should redirect to /pages/success (please see below my registrations_controller.rb )
routes.rb
App::Application.routes.draw do
resources :posts
resources :questions
get "users/:id", to: "users#show"
devise_for :users, :controllers => {:registrations => "registrations"}, :skip => [:sessions, :registrations]
as :user do
get 'login' => 'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
as :user do
get '/' => 'devise/registrations#new', :as => :new_user_registration
post 'register' => 'devise/registrations#create', :as => :user_registration
end
get "registrations/update"
get "pages/home"
get "pages/privacy"
get "pages/terms"
get "pages/success"
end
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
'/pages/success'
end
end
There are several potential issues you may have:
Skip
If you're skipping the registrations functionality, I'd imagine it would prevent Devise from calling your RegistrationsController?
I would personally do this (correct your routes):
#config/routes.rb
root to: "users#index" (where ever your "logged-in" page is)
devise_for :users, path: "", controllers: { sessions: "sessions", registrations: "registrations" }, path_names: { sign_in: 'login', password: 'forgot', confirmation: 'confirm', unlock: 'unblock', sign_up: 'register', sign_out: 'signout'}
This will give you the routes you need, and will route to the "authenticated" index page in your app, thus either showing the login or registration page for Devise
Definition
The other issue you may have is an incorrect definition of your Devise Registrations controller. We use this code in a very recent development app:
#app/controllers/registrations_controller.rb
class RegistrationsController < ::Devise::RegistrationsController
end
Perhaps you could try using the :: before your Devise::RegistrationsController to see if it calls?