Configuring routes in devise when only using omniauth for authentication - ruby-on-rails

I have built an application which allows a user to authenticate against Active Directory using omniauth-ldap. If this is a new user, the successful authentication creates a user for them based on information returned from AD. If the user already exists, it just logs them in. Users do not register for the application, they just log in with AD credentials. And I never want the user to log in with database credentials.
I can't figure out how to get rid of or change around some of the routes. For example if a user visits /sign_in they get the database authentication. And if the user visits sign_up they are taken to a page to register for the site. I would like for users that visit /sign_in to be taken to the LDAP login which is /users/auth/ldap. I think I need to make a custom route, but I'm not sure which controller I need to direct the user to. And I want to make the sign_up page go away entirely.
Right now I have a link that allows users to log in using ldap, and the path for that is user_omniauth_authorize_path(:ldap). I'm just not sure how to translate that into something that my config/routes.rb file understands. This is what I have in routes right now.
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
When I run rake routes I do not see any route for user_omniauth_authorize_path which I presume is because that route is being generated by devise. So I think I need to have my routes point to a devise controller, but I can't seem to find the right path.

Try to add
:skip => [:sessions, :registrations] to your routes.rb
Something like this:
devise_for :users, :skip => [:sessions, :registrations]
This How To article might be helpful, and also here is one more link to go through.

Related

Devise controller method not found using Alchemy CMS

I have a Rails 4.1.8 app which was developed by a third party; it already uses Devise with a User model. I want to use Alchemy just to manage a few mostly-static pages like "about us" etc.
So I followed the directions for installing Alchemy CMS 3.1.0-rc1 into the existing app, and set Alchemy.user_class_name in the initializer. I mounted Alchemy under the /p/ path for now to avoid collisions with my existing paths.
Everything seems to be working fine, except when I try to view an Alchemy page while logged out, my application.html.erb throws the following error:
undefined method `new_user_session_path' for
#<#<Class:0x007fe4c6833ee0>:0x007fe4cb5b8940>
This happens because my app uses new_user_session_path in application.html.erb, to show a Login link for the guest user. In the regular app, it works fine, and also works fine when I view an Alchemy page when logged in.
I'm not familiar enough with Devise and Alchemy to figure out where the problem exists. I'm guessing it's one of two things:
when there is no logged-in User, the app is creating a "guest" User (for access to other methods on the User model), and Devise doesn't know about this user so it doesn't create the new_user_session_path helper.
I have some problem in my routing, and because Alchemy is a mountable engine, there is maybe some logic in my application controller that isn't getting called.
I'd prefer to not post my entire routes.rb or application controller, but here's the relevant devise section from the former.
devise_for :users, :path => "auth", :path_names => { :sign_in => 'login', :sign_out => 'logout',
:password => 'secret', :confirmation => 'verification', :registration => 'register' },
:controllers => {
:registrations => "authentication",
:passwords => "passwords",
:omniauth_callbacks => "omniauth_callbacks",
:sessions => "sessions"
}
devise_scope :user do
# several get/post definitions here to change various urls
end
I don't think it's #2, because even if I define a devise_scope for a custom path like:
devise_scope :user do
get 'login', to: 'devise/sessions#new'
end
I get the same problem: it works in the main app, and when users are logged in, but not on Alchemy pages with guest User.
Common rails routing proxy problem. Since the Alchemy views don't know your main apps routes, you need to use the 'main_app' routing proxy object.
So calling 'main_app.new_user_session_path' should fix your problem.
Read more about routes in engines in this Rails guide: http://guides.rubyonrails.org/engines.html#routes

Devise users/sign_in and users/show action trouble

I user devise 3.4.0 under rails 4.1.0.
I want to add user detail page, so I made this route
get 'users/:id' => 'users#show', as: 'user'
But after this, when I access /users/sign_in path, it try to find the user show page.
How to write the right route?
What you did will actually "override" the devise routes (and i think this is the problem you are facing)
If you want to add another route in the scope of devise routes, you have to do something like :
devise_scope :user do
get '/users/:id' => 'users#show'
end
after
devise_for :users
Let me know if it solves the problem !

Devise authentication as API only

I'm trying to use Devise authentication with Angular.js. Everything is already working except I want to hide the server-side login form, i.e. the result of /users/sign_in GET request leaving only the possibility of /users/sign_in POST request. Is this possible?
I think you'll need to skip sessions in your devise_for call in routes.rb (or not even call it) and just stick to devise_scope for setting up your sign_in path. So, your routes.rb would look like this:
devise_for :users, :skip => :sessions
devise_scope do
post "/users/sign_in" => "devise/sessions#create"
delete "/users/sign_out" => "devise/sessions#destroy"
end

Restrict access or redirect devise path users/sign_in

I am creating a rails app. And i have login in such a way that, users are directed or redirect to /login for signing in. And when users/sign_in is passed the it embeds the sign in form into the application layout which i don't want users to see. Is there a way i can redirect users/sign_in to /login or restrict access to it ?
thanks
The following will replace the routes:
devise_for :model_name, :path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout'}
It will replace the users/sign_in and users/sign_out routes with login/logout, and the redirects will take them to those routes accordingly as well.
try this. It should works. ;)
config/routes.rb
# change :devise_model to :user, or :admin or any name of your devise model
devise_scope :devise_model do
get '/login' => "devise/sessions#new"
end
and you can use this in view like this.
link_to 'Login', login_url
PS. if you have override devise controller. You should tell the router first, and change devise controller to your override controller name.
go to this link and see Configuring controllers content.

error setting user_root for devise redirect

Goal
When user submits the devise "edit registration" form, I want to redirect to users#show rather than the site's root.
Problem
Following Devise's instructions has not worked. I don't want to customize the Devise controller, so I'm left with two suggested modifications to routes.rb, either
devise_for :users do
get 'users', :to => 'users#show', :as => :user_root
end
or
match 'user_root' => 'users#show'
The first redirects to http://localhost:3000/users after submitting the edit form, the second redirects to http://localhost:3000/user_root. Both give the same error, "Couldn't find User without an ID".
My users#show page normally works in the app, so it's not an error with the controller method (or view, of course). It seems to be a routing error. I have "resources :users" in my routes.rb file, nothing else regarding users. If I need to give more information please let me know!
Question
Why isn't the user id being passed in the url?
Have you considered making the action a member action? this will ensure that the url format is controller//action. I don't know if you can even use the member thing in the devise route definition, but that's where I'd start.
devise_for :users do
member do
get 'users', :to => 'users#show', :as => :user_root
end
end
The member part doesn't work but if you take that out it is working for me. +1 to jaydel for close enough :P
devise_for :users do
get 'users', :to => 'users#show', :as => :user_root
end
Another thing that I tried which also worked was :
devise_scope :user do
get 'users' => 'users#show', :as => :user_root
end

Resources