My Rails 5 app uses Devise 4.2 to handle authentication for Admin and Agent user types (separate tables). It's all working, except for having the root be different for a logged in Admin vs Agent
In routes.rb I can set authenticated_root for either :admin OR :user, but doing both (as shown below) causes the error when the app is started: ArgumentError: Invalid route name, already in use: 'authenticated_root'
It seems Rails is ignoring that the two uses of authenticated_root occur in two different scenarios.
# routes.rb
devise_for :agents, :skip => [:registrations]
as :agent do
get 'agents/edit' => 'devise/registrations#edit', :as => 'edit_agent_registration'
put 'agents' => 'devise/registrations#update', :as => 'agent_registration'
end
devise_for :admins, :skip => [:registrations]
as :admin do
get 'admins/edit' => 'devise/registrations#edit', :as => 'edit_admin_registration'
put 'admins' => 'devise/registrations#update', :as => 'admin_registration'
end
authenticated :admin do
root 'pages#adminhome', as: :authenticated_root
end
authenticated :agent do
root 'pages#agenthome', as: :authenticated_root
end
Should I perhaps be using some sort of lambda to set authenticated_root instead of the normal method documented, and used above?
I tried this approach instead but got the same error:
root :to => 'pages#adminhome', :constraints => lambda { |request| request.env['warden'].user.class.name == 'Admin' }, as: :authenticated_root
root :to => 'pages#agenthome', :constraints => lambda { |request| request.env['warden'].user.class.name == 'Agent' }, as: :authenticated_root