Devise authentication as API only - ruby-on-rails

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

Related

add your own custom route to devise

i've seen a bunch of posts on how to rename already-declared routes in devise. I want to expand devise to have my own route check for and idle session. I am implementing a simple js check every 1 minute that I want to hit 'check_active' in the devise sessions controller. I tried this but no luck:
devise_scope :sessions do
get 'check_active'
end
Is there way to expand devise with a custom route (not rename an already-existing one) ?
UPDATE - almost there, i did this
# already had this in routes
devise_for :users, :controllers =>
{ registrations: 'registrations',
confirmations: 'confirmations',
sessions: 'sessions',
passwords: 'passwords',
omniauth_callbacks: "omniauth_callbacks"}
# added this
devise_scope :sessions do
get '/check_active' => 'sessions#check_active'
end
I have a js firing, i have it get '/check_active' as rake routes shows this:
check_active GET /check_active(.:format)
But when it fires, the controller 404s with
AbstractController::ActionNotFound (Could not find devise mapping for path "/check_active".
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]
):
If you are overwriting Devise's default controllers, it is not any different from any other controller to add your own route.
After you create your devise controllers to overwrite, do the following:
Under sessions_controller declare a method
# app/controllers/devise/sessions_controller.rb
def check_active
# do what you want to do
end
And in your router:
# config/routes.rb
devise_scope :sessions do
get 'check_active', to: "devise/sessions#check_active"
end
I was trying the same thing and realized that the scope should be for user and not sessions, also ensure that it has to be singular.
devise_scope :user do
get '/check_active' => 'sessions#check_active'
end
Edit: Adding link to help docs for better understanding

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 Registration URL could not be found

I have a problem with devise after customizing the signup - route.
The devise doc mentions, that routes can easily be customized, so I tried to add a token to the URL to set up a easy invitation system. Ist really straight forward and all I did was adding
devise_for :users, :path_names => { :sign_up => "signup/:invitation_token" }
to my routes. A mailer sends an email with the token and inside I pass
new_user_registration_path(#invitation.token)
rake routes says
new_user_registration GET /users/signup/:invitation_token(.:format) devise/registrations#new
But I'm still getting
No route matches {:action=>"new", :controller=>"devise/registrations", :locale=>:de, :invitation_token=>nil}
I get it wether I pass the token or not...
I'm not shure what I'm missing.
Thanks in advance, hope someones sees what I'm doing wrong.
Greets, Rob
Check #invitation.token to make sure that it's not nil.
The error you're witnessing will occur on any view in which you pass nil into your new_user_registration_path link tag.
Bear in mind that you'll need to override the default behavior of Devise's users/registration controller in order to get your invitation system working correctly. Something like this would work:
# routes.rb
devise_for :users, :path_names => { :sign_up => "signup/:invitation_token" }, :controllers => {:registrations => "users/registrations"}
# app/controllers/users/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
# add custom create logic here
end
end

Configuring routes in devise when only using omniauth for authentication

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.

Devise map login page for multiple urls

I use devise for authentication. I have an admin user to play with the application. Is it possible to map the urls '/login' and '/admin' to the same login form?
Yes you can. Add the following to your routes.rb file, assuming your devise model is called 'admin':
devise_scope :admin do
get 'login' => 'devise/sessions#new'
get 'admin' => 'devise/sessions#new'
end
The normal '/admins/sign_in' route will still be available as well.

Resources