Search code examples
ruby-on-railsjsondevise

Devise login error doesn't return JSON


I have a Devise SessionController like :

class SessionsController < Devise::SessionsController
    # Respond only with json, no html
    respond_to :json
end

When the user give a bad login/password the response is a string from i18n file devise.en.yml

Invalid email or password.

I would like to have something like other validations errors

{
  "errors" : {
    "message" : "Invalid email or password"
  }
}

EDIT I found a solution by creating a custom failure app but I think Devise can do it without this custom failure, Right ?


Solution

  • I found the solution.

    In app/config/initializers/devise.rb I change :

    config.navigational_formats = [:json]
    

    to

    config.navigational_formats = []
    

    I really don't know why but... This helped : http://blog.andrewray.me/how-to-set-up-devise-ajax-authentication-with-rails-4-0/