如何使用设计登录用户? [英] How do I login a user with devise?

查看:107
本文介绍了如何使用设计登录用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的导轨应用程序,我正在遇到一个重大的问题与设计。我有一个控制器:

  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

问题是它永远不会记录用户,它总是停在这一行

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

我甚至把大量的记录器放在实际的宝石文件中,看看我能看到任何东西,但没有什么,我真的不知道如何解决这个问题。如果我评论这行,那么用户登录,但如果电子邮件不在数据库中,并且适用于任何密码(这绝对不是正确的解决方案),则失败。



如何解决这个问题?



更新



这个工作,但似乎非常黑客

 #POST / resource / sign_in 
def create
resource = User.find_by_email(params [:user] [:email])

redirect_to(new_user_session_path,:notice =>'无效的电子邮件地址或密码。密码区分大小写。)并返回if resource.encrypted_pa​​ssword.blank?
bcrypt = BCrypt :: Password.new(resource.encrypted_pa​​ssword)
password = BCrypt :: Engine.hash_secret(#{params [:user] [:password]}#{resource.class.pepper },bcrypt.salt)
valid = Devise.secure_compare(password,resource.encrypted_pa​​ssword)
#resource = warden.authenticate!(:scope => resource_name,:recall =># controller_path} #new)
如果有效
set_flash_message:notice,:signed_in
sign_in_and_redirect(resource_name,resource)
else
redirect_to(new_user_session_path,:notice => 电子邮件地址或密码无效,密码区分大小写)并返回
end

end


解决方案

如果要登录用户,请使用控制器操作中的 sign_in 帮助器:

  sign_in(:user,user)


I have my rails application and I am running into a major issue with devise. I have a controller:

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

The problem is it never logs the user in, it always stops at this line

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

I even put tons of loggers in the actual gem files to see if I could see anything off but nothing and I really have no idea how to fix this. If I comment this line out then the user gets logged in but fails if the email is not in the db and works for any password (which is definitely not the right solution)

How do I fix this?

UPDATE

this works but seems very 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

解决方案

If you want to sign in a user, use the sign_in helper inside your controller's action:

sign_in(:user, user)

这篇关于如何使用设计登录用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆