加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – warden custom_failure

发布时间:2020-12-17 02:16:40 所属栏目:百科 来源:网络整理
导读:我使用devise进行API身份验证.我已经覆盖了SessionController:create方法如下: class Users::SessionsController Devise::SessionsController def create resource = warden.authenticate!(:scope = resource_name,:recall = "#{controller_path}#new") se
我使用devise进行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!方法.

解决方法

您应该实现自己的失败应用程序.您可以在此处查看和/或继承设计的默认值
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读