2012-02-18 41 views
35

Tôi có ứng dụng đường ray của mình và tôi đang gặp phải vấn đề lớn với sự phát sinh. Tôi có một bộ điều khiển:Tôi làm cách nào để đăng nhập một người dùng?

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 

Vấn đề là nó không bao giờ đăng nhập người sử dụng trong, nó luôn luôn dừng lại ở dòng này

resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") 

tôi thậm chí đặt tấn logger trong các tập tin đá quý thực tế để xem liệu tôi có thể thấy bất cứ điều gì nhưng không có gì và tôi thực sự không có ý tưởng làm thế nào để sửa lỗi này. Nếu tôi nhận xét dòng này thì người dùng sẽ đăng nhập nhưng không thành công nếu email không nằm trong db và làm việc cho bất kỳ mật khẩu nào (chắc chắn không phải là giải pháp đúng)

Làm cách nào để khắc phục sự cố này?

CẬP NHẬT

làm việc này, nhưng có vẻ rất 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 

Trả lời

69

Nếu bạn muốn đăng nhập vào một người sử dụng, sử dụng sign_in helper bên trong hành động của bộ điều khiển của bạn:

sign_in(:user, user) 
+1

này ghi chúng vào bất kể ngay cả khi mật khẩu là sai hay không – Trace

+29

Kiểm tra ' user.valid_password? (params [: password]) 'trước tiên, sau đó. –

+0

Tôi đã thử chính xác như thế này nhưng có số lượng sai các đối số (2 cho 0). bất kỳ ý tưởng? – ishwr

3
resource = warden.authenticate!(:scope => resource_name) 
    sign_in(resource_name, resource) 
0

Tôi thấy bài đăng này hữu ích để thiết lập thông tin đăng nhập cho các thông số yêu cầu. https://makandracards.com/makandra/37161-rspec-devise-how-to-sign-in-users-in-request-specs

module DeviseRequestSpecHelpers 

    include Warden::Test::Helpers 

    def sign_in(resource_or_scope, resource = nil) 
    resource ||= resource_or_scope 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    login_as(resource, scope: scope) 
    end 

    def sign_out(resource_or_scope) 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    logout(scope) 
    end 

end 

Bao gồm nó trong spec_helper bạn

RSpec.configure do |config| 
    config.include DeviseRequestSpecHelpers, type: :request 
end 

Và đăng nhập khi cần thiết

sign_in create(:user, name: 'John Doe')

Các vấn đề liên quan