warden custom_failure

Mat*_*usz 5 ruby-on-rails devise warden ruby-on-rails-3

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 
Run Code Online (Sandbox Code Playgroud)

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."}
Run Code Online (Sandbox Code Playgroud)

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

pol*_*iro 3

您应该实现自己的失败应用程序。您可以在此处查看和/或继承 devise 的默认设置 https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb

要进行设置,请在 config/initializers/devise.rb 配置文件中:

Devise.setup do |config|
  config.warden do |manager|
    manager.failure_app = CustomFailureApp
  end
end
Run Code Online (Sandbox Code Playgroud)