【发布时间】:2017-09-24 15:25:58
【问题描述】:
我使用 Devise 使用重写的控制器 Sessions 和方法 create 进行 Ajax 登录:
class SessionsController < Devise::SessionsController
def create
resource = User.find_for_database_authentication(email: params[:user][:email])
unless resource
render json: 0
end
if resource.valid_password?(params[:user][:password])
sign_in :user, resource
return render json: 1
end
end
end
登录正常,但如果我尝试使用错误的密码,它不会增加列failed_attempts。
我在模型User 中有:lockable 和表中的所有列。
失败的尝试可以正常工作,无需覆盖 Sessions 类。
我知道,我需要使用一些方法或重写find_for_database_authentication 方法,但我不知道这个方法的名称是什么。
【问题讨论】:
-
尝试
resource.active_for_authentication?而不是valid_password? -
即使密码错误,每次返回1(登录成功)。可能是我做错了什么。我应该在某处传递密码
params[:user][:password]吗? -
看看这个github.com/thanhluanuit/devise_ajax_example/tree/… 我认为你的控制器中的代码应该调用warden。
-
我的错。
active_for_authentication?在认证后被调用。应该改用“#valid_for_authentication?”。 -
相同,使用无效密码登录。
标签: ruby-on-rails ruby devise rubygems