简体   繁体   English

如何在Rails(Devise gem)中查看当前密码?

[英]How to check current password in Rails (Devise gem)?

I'm failing to check current password using jQuery validation. 我没有使用jQuery验证检查当前密码。 I'm using Devise gem and jQuery validation. 我正在使用Devise gem和jQuery验证。 Firebug says: 萤火虫说:

the problem is in my controller : 问题出在我的控制器中:

def checkpass
 user = User.find_by_email(params[:user][:email])
 respond_to do |format|
 if user && user.authenticate(params[:user][:password])
    format.json { render :json => @user }
 end

end 结束

with this code Firebug gives me error 使用此代码,Firebug给了我错误

 406 Not Acceptable

end

example of checkemail uniqueness action: checkemail唯一性动作的例子:

 def checkemail
  @user = User.find_by_email(params[:user][:email])
   respond_to do |format|
   format.json { render :json => !@user }
  end
end

My js: 我的js:

$("#edit_password").validate({
   "user[current_password]":{
            required: true,
            minlength: 6,
            remote: {
          url: "http://127.0.0.1:3000/checkpass"
          }
 ...
 })

and HTML: 和HTML:

   <input type="password" size="30" name="user[current_password]" id="user_current_password" class="valid">

Can someone suggest how to do it ? 有人可以建议怎么做吗?

I am using rails version 4 and when I tried doing user.authenticate I got error saying unknown methods, so I found the other method for checking if password is valid, 我正在使用rails版本4,当我尝试使用user.authenticate时,我收到错误说未知方法,所以我找到另一种方法来检查密码是否有效,

valid_password?(current_password)

I thought to share it so it might help someone else as well. 我想分享它,所以它也可以帮助别人。

in the controller in function check_pass 在函数check_pass中的控制器中

  user = User.find_by_email(params[:user][:email])
  respond_to do |format|
     if user && user.authenticate(params[:user][:password])
        format.json { render :json => @user }
     end
  end

Password is not stored in plain text format it's a salted hash,so user can't find using password. 密码不以纯文本格式存储,它是盐渍哈希,因此用户无法找到使用密码。

You can authenticate password is valid or not so salted hash work like 你可以验证密码是否有效,所以盐渍哈希工作就好

  • crypted string = plaintext(password)+hashing algo(encrypt) crypted string = plaintext(password)+ hashing algo(encrypt)

  • some random string(salt) +crypted string then checked for match You can use warden.authenticate or authenticate (check docs) method on user to verify password 一些随机字符串(盐)+加密字符串,然后检查匹配您可以使用warden.authenticateauthenticate (检查文档)方法用户验证密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM