Development Extension for JSON API Support

We are updating an application that has two ways to authenticate users.

  • Controllers that have a before_filter :authenticate_user! and available as a standard Rails site.
  • Controllers inside the API module accessed through JSON by client applications. All routes are prefixed with /api/session_id/ , with the exception of an authentication action that has only the /api prefix.

Both methods authenticate users using the User model.

Is there a way to configure the application to support both of them? How?

Note. I do not want to create users through JSON. I just want to authenticate them.

+4
source share
2 answers

Finished with the creation of a new article for the overseer .

In the valid? method valid? I am checking if params[:controller] comes from the api namespace. This way, I did not deal with default authentication with http.

0
source

I did this by overriding the development session controller.

Add an entry to the routes for the custom session controller:

  devise_for :users, :controllers => {:sessions => 'sessions'} 

And redefine the session controller:

  class SessionsController < Devise::SessionsController def create resource = warden.authenticate!(:scope => resource_name, :recall => "sessions#failure") return sign_in_and_redirect(resource_name, resource) end def sign_in_and_redirect(resource_or_scope, resource=nil) scope = Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope sign_in(scope, resource) unless warden.user(scope) == resource respond_with do |format| format.json {render :json => {:success => true} } format.any {super} end end def failure respond_with do |format| format.json {render:json => {:success => false, :errors => ["Login failed."]} } format.any {redirect_to :back, :notice => "Wrong Email / Password" } end #return render:json => {:success => false, :errors => ["Login failed."]} end end 

" Rails and Devise: Override SessionsController discusses the same issue more.

0
source

All Articles