【问题标题】:intercept devise active_for_authentication error message拦截设计 active_for_authentication 错误消息
【发布时间】:2014-05-05 13:00:12
【问题描述】:

我正在尝试实现“禁用用户”功能。从各种来源我读到,可以在我的 user.rb 中进行以下操作

  def active_for_authentication?
    super && disabled_at.blank?
  end

  def inactive_message
   'Your account has been disabled.'
  end

这很好用,在用户被禁用后,它会将他注销并指向登录页面,并显示正确的非活动消息。

但是,当用户尝试登录(通过 ajax)时,会发生以下情况:

请求网址:http://localhost:3000/users/sign_in 请求方法:POST 状态码:302 暂时移动

但实际上在服务器控制台中

在 132 毫秒内完成 401 Unauthorized

这是我的 session_controller.rb

def create
  self.resource = warden.authenticate!(scope: resource_name, recall: "#{controller_path}#failure_with_ajax")
  sign_in(resource_name, resource)
  trial_mode_days_left?(resource)
  render json: { redirect: root_path }, status: :ok
end

def failure_with_ajax
   render json: { error: t('devise.failure.invalid') }, status: :unprocessable_entity
end

问题是当用户被禁用时,failure_with_ajax 永远不会被调用,但是当输入的凭据错误时它可以正常工作。不知道为什么 active_for_authentication 为 false 时没有触发召回,以及如何将 inactive_message 传递给前端的用户。

【问题讨论】:

    标签: ruby-on-rails ruby devise


    【解决方案1】:

    这是因为当调用 active_for_authentiction 时warden.authenticate 不会抛出带有选项的失败响应。它只是调用失败的超级方法,并且不会抛出给出“recall”的选项。我找到了一个补丁。您必须覆盖该设计。解决办法是here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-07
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多