Redirect to appropriate pages after logging in - ruby-on-rails

I have installed devise and activeadmin gem. When I try to login via client then I get redirected to the right page but when I try to login via admin I get redirected to the client login page.
routes.rb
namespace :admin do
# get "/stats" => "stats#stats"
devise_scope :admin_user do
get '/stats/:scope' => "stats#stats", as: :admin_stats
end
end
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
namespace :client do
get 'dashboard' => 'dashboard#index', as: 'dashboard'
end
devise_for :users, class_name: 'FormUser', controllers: { omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations' }
devise_scope :user do
root to: 'devise/registrations#new'
end
application_controller.rb
def after_sign_in_path_for(resource_or_scope)
client_dashboard_path
end
def after_sign_out_path_for(resource_or_scope)
root_path
end
How can I fix this?
Here is a link to the test app enter link description here
Admin login enter link description here

def after_sign_in_path_for(resource_or_scope)
case resource_or_scope
when AdminUser
admin_dashboard_path
when User
client_dashboard_path
end
end

Related

How to set rails devise sign_in path as a root url

devise_for :admins, path: 'admins'
devise_scope :admin do
root to: "devise/sessions#new"
end
http://localhost:3000/
I want to redirect admins/sign_in path when I just enter above url, login page is opening sometimes but after click on login button every time I get this error and not sign in. How to solve this problem?
error:
Filter chain halted as :require_no_authentication rendered or
redirected
look like you're trying to log in the same user again without a sign out
devise_for :admins, path: 'admins'
devise_scope :admin do
authenticated :admin do
root 'home#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
You can achieve the same with this
routes.rb
root "home#index"
devise_for :admins, path: 'admins'
home_controller.rb
class HomeController < ApplicationController
def index
if not admin_signed_in?
redirect_to admin_session_path
end
end
A logged user can't sign in again...
You can try this, in your session_controller.rb add
class Users::SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
end

How to redirect to another page after devise sign in?

First, see my routes :
Rails.application.routes.draw do
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
devise_for :users, controllers: {
registrations: "registrations",
sessions: "sessions"
}
devise_scope :user do
authenticated :user do
root 'appointments#index', as: :authenticated_root
end
unauthenticated do
root 'sessions#new', as: :unauthenticated_root
end
match '/logout', :to => 'devise/sessions#destroy', via: :all
end
resources :appointments do
get :available_slots, on: :collection
resources :notes
resources :images, only: [:show]
end
#patch 'appointments/:id' => "appointments#update_status", as: :update_status
match 'appointments/:id/update_status' => "appointments#update_status", :via => :post
match 'appointments/:id/visited_patient_appointment' => "appointments#visited_patient_appointment", :via => :post
get 'archive' => "appointments#archive"
end
Now, how to redirect to appointments_path after user sign in? There is one devise method called after_sign_in_path_for(resource) which I override in Appointments Controller but still it is not working.
You trying to override in Appointments Controller which is wrong, it will be sessions_controller.rb or application_controller.rb
Try the following in the sessions_controller.rb or application_controller.rb
protected
def after_sign_in_path_for(resource)
stored_location_for(resource) || appointments_path
end
If not have stored_location then he will redirect to appointments_path
If you need to redirect all time to the same page like appointments_path then
protected
def after_sign_in_path_for(resource)
appointments_path
end
See the Devise wiki

Rails, Active Admin, Devise, routes

I've been working with Rails, Active Addmin and cancancan. Everything is working fine except one thing. Recently I have added separate namespaces for my admin type users and clients.
Before that change, I redirected all authenticated users to the same active admin dashboard in such way (routes.rb):
devise_scope :user do
authenticated :user do
root :to => 'admin/dashboard#index', as: :authenticated_root
end
unauthenticated :user do
root :to => 'pages#index', as: :unauthenticated_root
end
end
Currently I need to somehow add additional condition that will be checking if authenticated user has role admin or client.
My idea was to make sth like that:
devise_scope :user do
authenticated :user do
if current_user.role?(:Architect) || current_user.role?(:Admin)
root :to => 'admin/dashboard#index', as: :authenticated_root
else
root :to => 'clients/dashboard#index', as: :authenticated_client
end
end
unauthenticated :user do
root :to => 'pages#index', as: :unauthenticated_root
end
end
But I am getting error: undefined local variable or method `current_user'
Does anybody know how I can check user's role in routes? Is there any better way to do that?
The root is a config file responsible for routes definition, you can't access any session variables in config files.
If you want to redirect user after sign in you can use after_sign_in_path_for method in Devise::SessionsController
class SessionsController < Devise::SessionsController
def after_sign_in_path_for(resource)
if resource.role?(:Architect) || resource.role?(:Admin)
authenticated_root_url
else
authenticated_client_url
end
end
end
In your route you need to indicate the custom sessions_controller
devise_for :user, :controllers => {:sessions => "sessions"}
Page Controller :
class PageController < ApplicationController
before_filter :check_route, :only => [:index]
def check_route
return unless user_signed_in?
if current_user.role?(:Architect) || current_user.role?(:Admin)
redirect_to :controller => 'admin/dashboard', :action => 'index'
else
redirect_to :controller => 'clients/dashboard', :action => 'index'
end
end
end
routes.rb:
root :to => 'pages#index', as: :unauthenticated_root

show errors in custom page rather than in default page while login/signup with devise

I have put login and signup in one page and every thing works fine except when I encounter errors. Then the page redirects to their default pages and show errors there. In my case the login redirects me to the default domain.com/users/sign_in , but signup redirects me to domain.com/users.
routes.rb
Rails.application.routes.draw do
root 'visitor#index'
namespace :admin do
# get "/stats" => "stats#stats"
devise_scope :admin_user do
get '/stats/:scope' => 'stats#stats', as: :admin_stats
end
end
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
namespace :client do
get 'dashboard' => 'dashboard#index', as: 'dashboard'
# resources :verification, only: [:create, :index, :destroy]
get 'verification' => 'verification#index', as: 'verification'
match 'verification' => 'verification#upload', as: 'verification_upload', via: [:post, :patch]
end
devise_for :users, class_name: 'FormUser', controllers: { omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations' }
# devise_scope :user do
# root to: 'devise/registrations#new'
# end
end
you can use a CustomFailure class to control where the redirect goes if Devise fails to authenticate.
It's explained at this wiki page...
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

Redirect user to Sign In page after Sign Up (registration)

I am using Devise 3.1.1 and am trying to redirect user to the Sign In page after he signs up.
As instructed in Devise's wiki I overridden RegistrationsController with the following:
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
'/users/sign_in'
end
end
As instructed, I also added the following line to the routes.rb:
devise_for :users, controllers: { registrations: 'registrations'}
After which I get the following error when I go to sign in page:
Invalid route name, already in use: 'new_user_session' You may have defined two routes with the same name using the:asoption, or you may be overriding a route already defined by a resource with the same naming.
In my routes I already have this defined:
devise_for :users, skip: :registrations
devise_scope :user do
resource :registration,
# disabled :edit & :destroy
only: [:new, :create, :update],
path: 'users',
path_names: { new: 'sign_up' },
controller: 'devise/registrations',
as: :user_registration do
get :cancel
end
end
You can only define the devise_for block once, and as you're already messing with the default registrations controller you should be able to just do something like the following to have devise use your controller:
devise_for :users, skip: :registrations
devise_scope :user do
resource :registration,
# disabled :edit & :destroy
only: [:new, :create, :update],
path: 'users',
path_names: { new: 'sign_up' },
controller: 'registrations',
as: :user_registration do
get :cancel
end
end

Resources