典狱长custom_failure
我使用 device 进行 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!
方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该实现自己的失败应用程序。您可以在此处查看和/或继承设备的默认设置
https://github.com/plataformatec/devise/blob/master /lib/devise/failure_app.rb
要进行设置,请在 config/initializers/devise.rb 配置文件中:
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:
在您的语言环境文件(config/locales/ 目录中的文件)上,根据您的 i18n 配置(对于英语,它是
en.yml
)添加以下内容:当然,将
My custom message 替换为您想要的消息的登录无效
。On your locale file (a file on config/locales/ directory), depending of your i18n configuration (for english it is
en.yml
) add this:Of course, substitute
My custom message for invalid login
to the message you want.