title: Using devise to authenticate a API login request via JSON
body: <p>I'm trying to use devise to authenticate a simple email/password login so users can access the rest of the API via auth tokens. I'm running into trouble with devise simply returning <code>You need to sign in or sign up before continuing.</code> Here's my code:</p>
  
  <pre><code>class LoginController &lt; ApplicationController
    respond_to :json
  
    def login
      resource = warden.authenticate!(:scope =&gt; resource_name, :recall =&gt; "#{controller_path}#failure")
      render :status =&gt; 200,
            :json =&gt; { :success =&gt; true,
                    :info =&gt; "Logged in",
                    :user =&gt; current_user
         }
    end
  
    def failure
      render :status =&gt; 401,
         :json =&gt; { :success =&gt; false,
                    :info =&gt; "Login Failed",
                    :data =&gt; {} }
    end
  
    def resource_name
      :user
    end
  
    def resource
      @resource ||= User.new
    end
  
    def devise_mapping
      @devise_mapping ||= Devise.mappings[:user]
    end
   end
  </code></pre>
  
  <h3>routes.rb</h3>
  
  <pre><code>  devise_scope :user do
      post 'register' =&gt; 'registration#create', :as =&gt; :registers
      post 'login' =&gt; 'login#login', :as =&gt; :login
    end
  </code></pre>
  
  <p>I'm sending the following post data:</p>
  
  <pre><code>{ 
    "user" : {
      "email" : "testPost4@fake.com",
      "password" : "Password1"
    }
  }
  </code></pre>
  
  <p>Having browsed various posts I've added:</p>
  
  <pre><code>config.navigational_formats = [:json]
  </code></pre>
  
  <p>to the devise.rb file but it didn't solve the problem.</p>
  
  <p>Adding <code>skip_before_filter :authenticate_user!</code> doesn't work either.</p>