Mat*_*iby 35 ruby ruby-on-rails devise warden ruby-on-rails-3
我有我的rails应用程序,我遇到了一个设计的主要问题.我有一个控制器:
class Users::SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
include Devise::Controllers::InternalHelpers
def new
clean_up_passwords(build_resource)
respond_to do |format|
format.html { render :layout => "sessions" }
format.mobile
end
end
# POST /resource/sign_in
def create
resource = User.find_by_email(params[:user][:email])
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message :notice, :signed_in
sign_in_and_redirect(resource_name, resource)
end
end
Run Code Online (Sandbox Code Playgroud)
问题是它永远不会记录用户,它总是停在这一行
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
Run Code Online (Sandbox Code Playgroud)
我甚至在实际的宝石文件中放了大量的记录器,看看我是否可以看到任何东西但没有任何东西,我真的不知道如何解决这个问题.如果我注释掉这一行,那么用户就会登录,但如果电子邮件不在数据库中并且适用于任何密码(这绝对不是正确的解决方案),则会失败
我该如何解决?
UPDATE
这有效,但似乎非常hackish
# POST /resource/sign_in
def create
resource = User.find_by_email(params[:user][:email])
redirect_to(new_user_session_path, :notice => 'Invalid Email Address or Password. Password is case sensitive.') and return if resource.encrypted_password.blank?
bcrypt = BCrypt::Password.new(resource.encrypted_password)
password = BCrypt::Engine.hash_secret("#{params[:user][:password]}#{resource.class.pepper}", bcrypt.salt)
valid = Devise.secure_compare(password, resource.encrypted_password)
# resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
if valid
set_flash_message :notice, :signed_in
sign_in_and_redirect(resource_name, resource)
else
redirect_to(new_user_session_path, :notice => 'Invalid Email Address or Password. Password is case sensitive.') and return
end
end
Run Code Online (Sandbox Code Playgroud)
Rya*_*igg 74
如果要登录用户,请使用sign_in控制器操作中的帮助程序:
sign_in(:user, user)
Run Code Online (Sandbox Code Playgroud)