title: Json Authentication to Devise
body: <p>I am trying to setup json based authentication on my current rails app. The app's authentication is currently handled by devise.</p>
  
  <p>I have read couple of questions on stackoverflow, but I do not seem to get it working.</p>
  
  <p><strong>routes.rb:</strong></p>
  
  <pre><code>devise_for :users, :controllers =&gt; { :omniauth_callbacks =&gt; "users/omniauth_callbacks", :sessions =&gt; 'users/sessions' }
  </code></pre>
  
  <p><strong>SessionsController:</strong></p>
  
  <pre><code>class Users:: SessionsController &lt; Devise::SessionsController
    def create
      resource = warden.authenticate!(:scope =&gt; resource_name, :recall =&gt; :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
      return render :json =&gt; {:success =&gt; true, :redirect =&gt; stored_location_for(scope) || after_sign_in_path_for(resource)}
    end
  
    def failure
      return render:json =&gt; {:success =&gt; false, :errors =&gt; ["Login failed."]}
    end
  end
  </code></pre>
  
  <p>Not sure where I have gone wrong. The call I am making is:</p>
  
  <p><strong>JSON:</strong></p>
  
  <pre><code>Content-Type: application/json
  Accept: application/json
  data: {user: {password: mypass, email: some_email@gmail.com}}
  </code></pre>
  
  <p><strong>The Error:</strong></p>
  
  <pre><code>MultiJson::DecodeError (756: unexpected token at '{user: {password: mypass, email:    some_email@gmail.com}}'):
  2012-06-30T18:10:10+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/json-1.6.5/lib/json/common.rb:148:in `parse'
  </code></pre>