Suggest a feature

Welcome to TagMyCode

Please login or create account to add a snippet.
Language: Ruby
Posted by: Adam Bedell
Added: Aug 16, 2013 1:24 AM
Views: 85
Tags: api rails testing
But if an authentication error occurs, the control goes to warden which uses a failure_app which is nothing but a Rack application which sets the flash messages and renders/redirects based on the requested format. I need a custom json response with an errors key holding an array of errors instead of the default error key. So I added a custom_auth_failure_app.rb under config/initializers with the following content :
  1. class CustomAuthFailure < Devise::FailureApp
  2.   def respond
  3.     self.status = 401
  4.     self.content_type = 'json'
  5.     self.response_body = {"errors" => ["Invalid login credentials"]}.to_json
  6.   end
  7. end