Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 561
Ludovic Avatar asked Jul 28 '26 12:07

Ludovic


1 Answers

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/

like image 63
Ludovic Avatar answered Jul 30 '26 04:07

Ludovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!