Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warden custom_failure

I use devise for API authentication. I have overwritten SessionController:create method like this:

class Users::SessionsController < Devise::SessionsController

  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    val = sign_in(resource_name, resource)
    current_user.reset_authentication_token!

    respond_to do |format|
      format.html do
        respond_with resource, :location => redirect_location(resource_name, resource)
      end
      format.json do
        render :json => { :status => OK_OK, :auth_token => current_user.authentication_token }.to_json, :status => :ok
      end
    end
  end
end 

As you can see I respond with a status code when authenticating with JSON. When authentication fails I receive the following response:

{"error":"Invalid email or password."}

How do I change this message? I have no idea where to overwrite this warden.authenticate! method.

like image 942
Mateusz Avatar asked Oct 09 '22 05:10

Mateusz


2 Answers

You should implement your own failure app. You can look at and/or inherit from devise's default here https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb

To set it up, in your config/initializers/devise.rb config file:

Devise.setup do |config|
  config.warden do |manager|
    manager.failure_app = CustomFailureApp
  end
end
like image 66
polmiro Avatar answered Oct 13 '22 10:10

polmiro


On your locale file (a file on config/locales/ directory), depending of your i18n configuration (for english it is en.yml) add this:

en:
  devise:
    invalid: 'My custom message for invalid login'

Of course, substitute My custom message for invalid login to the message you want.

like image 40
Rodrigo Flores Avatar answered Oct 13 '22 10:10

Rodrigo Flores