3

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 ?

Ludovic
  • 1,992
  • 1
  • 21
  • 44
  • 1
    Have you read this: http://stackoverflow.com/questions/5973327/using-devise-1-3-to-authenticate-json-login-requests/8402035#8402035? It's old, but might give you some ideas. – A5308Y Feb 22 '15 at 07:28
  • Yep, I know that I could override the SessionController but Devise can't manage it alone without override controllers ? It's worked for validations, Devise return JSON why not for login errors ? – Ludovic Feb 22 '15 at 09:39
  • In your devise initializer did you mention to respond json. config.navigational_formats = ["*/*", :html, :json] – coderhs Feb 25 '15 at 05:30
  • @Coderhs it's like : `config.navigational_formats = [:json]` because I only need json – Ludovic Feb 25 '15 at 17:07
  • you login form could be making a request for js instead of json. – Joel Feb 26 '15 at 23:49
  • There is no form it's an API it's why I need JSON – Ludovic Feb 27 '15 at 18:14
  • Could you add the development.log entries from a sign in request? Just to make sure the issue is actually on the Devise side of things, and not due to the request being somehow mangled. – janfoeh Feb 28 '15 at 11:21

2 Answers2

4

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/

Ludovic
  • 1,992
  • 1
  • 21
  • 44
  • Man, in my app, any unnauthorized request stopped rendering default html or Json data – Crystian Leão Apr 08 '16 at 16:47
  • @CrystianLeão Yeah, you have to handle errors. Look here : http://stackoverflow.com/questions/22414524/how-can-i-make-laravel-return-a-custom-error-for-a-json-rest-api – Ludovic Apr 08 '16 at 18:33
1

Can you reproduce your problem use curl and provide output? In my case all works fine and I receive JSON.

$  maxd git:(master) ✗ curl -X POST --form "user[email]=a" --form "user[password]=password" 'http://localhost:3000/users/sign_in.json'
{"error":"Invalid email or password."}

Seems like the problem on your side.

Maxim
  • 9,701
  • 5
  • 60
  • 108
  • curl -X POST --form "user[email]=a" --form "user[password]=password" 'http://host:3000/users/sign_in' translation missing: fr.devise.failure.user.not_found_in_database. – Ludovic Feb 28 '15 at 23:22
  • It's strange, what is your devise version ? I didn't change anything on devise :( – Ludovic Feb 28 '15 at 23:23
  • 1
    1) Try to specify `.json` format (look at the end of my url) 2) Check your localization file `config/locales` or version of `devise-i18n` gem. Seems like localization file or gem version is outdated and incompatible with your devise version. This is cause internal error. Devise doesn't handle it and you see plain text with this error. – Maxim Mar 01 '15 at 08:00
  • With the `.json` I got an error : `NoMethodError (undefined method `flash' for #)`. No change without the .json but I have the i18 message (I deleted the i18 before). All gems are last version from rubygems – Ludovic Mar 01 '15 at 10:37
  • 1
    Could you please provide full backtrace? What about problems with localization file or gem? – Maxim Mar 01 '15 at 10:38
  • I did found a solution by creating a Custom Failure App, I'm updating my question – Ludovic Mar 01 '15 at 10:44
  • I deleted the devise.en.yml so it's why I got this "translation missing" error message. Here the full trace : http://pastebin.com/GvTBde8N – Ludovic Mar 01 '15 at 10:48
  • 1
    In may case I don't have custom failure application or additional devise extensions. – Maxim Mar 01 '15 at 10:48
  • Yes right it's why I got a pb :) Do you have any idea ? I'll try to make a new rails project and see – Ludovic Mar 01 '15 at 10:51
  • 1
    `undefined method flash ` error can be related to rails-api gem. Are you using it? – Maxim Mar 01 '15 at 11:02
  • Nope ! I just make a new rails4+devise3 project from scratch, I got the same problem... Devise doesn't return JSON. – Ludovic Mar 01 '15 at 11:06
  • I found my answer. Thanks for your help ! – Ludovic Mar 01 '15 at 11:49