【发布时间】: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