I have done some research and seen that this question has already been addressed several times, in different places:
Devise with rails 4 authenticated root route not working
Different '/' root path for users depending if they are authenticated (using devise)
Authenticated Route Constraints
Rails Tip #5: Authenticated Root and Dashboard Routes With Devise
I tried to follow the examples given in the links above and came up with the following solution, in my routes.rb file:
root to: 'pages#home'
devise_for :users, :path => 'account', controllers: { registrations: "registrations", confirmations: "confirmations" }
authenticated :user do
root 'calendars#index', as: :authenticated_root
end
The goal here is to direct unauthenticated users to the regular home page of the website, while authenticated users will go to their dashboard, inside the app, i.e. the calendars#index route, defined as follows in my routes:
calendars GET /calendars(.:format) calendars#index
However, when I log in as a user, and visit http://localhost:3000/, I keep landing on the regular home page of the website, instead of the user's dashboard inside the app.
What am I doing wrong?
Change routes.rb so that the unauthenticated root route is wrapped, just like the authenticated one:
devise_for :users, :path => 'account', controllers: { registrations: "registrations", confirmations: "confirmations" }
authenticated :user do
root 'calendars#index', as: :authenticated_root
end
unauthenticated :user do
root 'pages#home', as: :unauthenticated_root
end
Related
In my admin panel the logout link is not working. In my project I have removed the default admin user that is generated while installing the active admin and used the role from profile model to sign in as super_admin. My logout link is not working though. My /config/initializers/active_admin.rb is:
...
config.logout_link_path = :destroy_user_session_path
config.logout_link_method = :delete
...
My routes.rb file is given as:
Rails.application.routes.draw do
# Devise routes for registration, session and manual routes for confirmation and password
devise_for :users, controllers: {
confirmations: "users/confirmations",
passwords: "users/passwords"
} do
root to: "devise/sessions#new"
get '/users/sign_out', :to => 'devise/sessions#destroy'
end
# Route for super admin
ActiveAdmin.routes(self)
end
Thanks in advance for your input!!!
I got my logout link working by adding the active_admin.js file as I have deleted the file initially, as I thought it was not doing anything.
I'm using OAuth 2 gem to authenticate via google and facebook.
I need to do logout from google and facebook when I logout from my application. In OA documentation said to:
devise_scope :user do
delete 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session
end
Add this to routes.rb. I did it so, my routs rb now looks like:
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'callbacks' }
devise_scope :user do
delete 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session
end
When I add this line, i got an error when i try to rails s my application:
/Users/damirik/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/route_set.rb:507:in add_route': Invalid route name, already in use: 'destroy_user_session' (ArgumentError)
You may have defined two routes with the same name using the:as` option, or you may be overriding a route already defined by a resource with the same naming.
I really dont understand how to fix it. Help please
Looking at the devise_for method documentation, I can see that it already adds the exact delete 'sign_out' route, which makes it redundant.
This should be enough to make your code work.
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'callbacks' }
end
I just set up Devise and things are working properly. However, for some reason, my page is not working together in authentication.
Here is my routes.rb file.
devise_for :users #, path_names: { sign_out: 'sign_out' }
devise_scope :user do
authenticated :user do
root 'home#index'#, as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
get "/dashboard" => "home#index"
When I type in localhost:3000, it redirects me to the devise sign in page that exists on localhost:3000/users/sign_in.
When I type in localhost:3000/dashboard, it takes me to the sign in page as intended but when I enter my credentials, it keeps on bringing me back to the same sign in page over and over again.
Here is my application_controller.rb file.
protect_from_forgery with: :exception
before_filter :authenticate_user!
My intention is to have localhost:3000 redirect me to an authenticated version of localhost:3000/dashboard where I can see everything on that page. Not sure what's happening.
I suggest you to rewrite your code:
Remove before_filter :authenticate_user! as problem with unauthorized users already handled by routes.
Remove devise_scope :user do ... end block - there is a simpler solution (below).
Your routes.rb :
authenticated :user do
root to: redirect('/dashboard'), as: :authenticated_root
end
root to: redirect('/users/sign_in')
get "dashboard" => "home#index"
I have 2 devise models which is fine as they capture different values and i felt separating them was the best solution instead of using roles.
That being said, I am trying to root to different views. Here is my routes file:
devise_for :patients, controllers: {
sessions: 'patients/sessions',
registrations: 'patients/registrations'
}
as :patient do
authenticated do
root to: 'dashboard#patient'
end
unauthenticated do
root to: 'shared#home', as: 'unauthenticated_root'
end
end
devise_for :pharmacists, controllers: {
sessions: 'pharmacists/sessions',
registrations: 'pharmacists/registrations'
}
as :pharmacist do
authenticated do
root to: 'dashboard#pharmacist'
end
unauthenticated do
root to: 'shared#home', as: 'unauthenticated_root'
end
end
This is the error message:
Invalid route name, already in use: 'root'
You may have defined two routes with the same name using the `:as` option,
or you may be overriding a route already defined by a resource with the
same naming.
What would be the best way to alleviate this issue ?
You can achieve the same thing using the below hacks without the devise_scope
authenticated :patient do
root to: "patients#show", as: :authenticated_patient
end
authenticated :pharmacist do
root "pharmacists#index", as: :authenticated_pharmacist
end
unauthenticated do
root "pages#home", as: :unauthenticated_user
end
The issue stems from the fact that i was not using "as:" on the actual roots so they end up having the same endpoints. after fixing this issue the correct code looks like this:
devise_for :patients, controllers: {
sessions: 'patients/sessions',
registrations: 'patients/registrations'
}
devise_scope :patient do
authenticated do
root to: 'dashboard#patient', as: 'authenticated_patient_root'
end
unauthenticated do
root to: 'shared#home', as: 'unauthenticated_patient_root'
end
end
devise_for :pharmacists, controllers: {
sessions: 'pharmacists/sessions',
registrations: 'pharamacists/registrations'
}
devise_scope :pharmacist do
authenticated do
root to: 'dashboard#pharmacist', as: 'authenticated_pharmacist_root'
end
unauthenticated do
root to: 'shared#home', as: 'unauthenticated_pharmacist_root'
end
end
I'm new to rails and I'm using devise to authenticate users.
I have a devise User model and I'm using devise views.
my routes.rb file goes like this
Freshconnection::Application.routes.draw do
root to: 'pages#home'
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users do
ActiveAdmin.routes(self)
get '/users/sign_out' => 'devise/sessions#destroy'
end
As of now when I run the server, the request is routed to pages#home which is my landing page.
I want the users/sign_up page to be the landing page so that the user can sign_up and start using the website.
Please guide me on how this has to be accomplished.
Many thx
As described here (link) you could try the workaround
devise_scope :user do
root to: "devise/registrations#new"
end
Change your root directive to:
root to: 'devise/registrations#new'