5

我使用设计进行 API 身份验证。我已经覆盖SessionController:create了这样的方法:

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 

如您所见,在使用 JSON 进行身份验证时,我会使用状态代码进行响应。当身份验证失败时,我收到以下响应:

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

如何更改此消息?我不知道在哪里覆盖这个warden.authenticate!方法。

4

2 回答 2

3

您应该实现自己的失败应用程序。您可以在此处查看和/或继承设计的默认值 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
于 2013-07-31T00:12:53.760 回答
1

在您的语言环境文件(config/locales/ 目录上的文件)上,根据您的 i18n 配置(对于英语是en.yml)添加以下内容:

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

当然,替换My custom message for invalid login为您想要的消息。

于 2011-12-17T20:22:38.687 回答