2012-09-26 49 views
3

我无法使用jQuery验证检查当前密码。我正在使用Devise gem和jQuery验证。萤火虫说:如何检查Rails中的当前密码(Devise gem)?

的问题是在我的控制器:

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 

与此代码萤火虫给我的错误checkemail独特作用

406 Not Acceptable 

end 

例如:

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

我的JS:

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

和HTML:

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

有人建议可以怎么办呢?

控制器

回答

4

在功能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 
+0

'语法错误,意想不到tIDENTIFIER,期望 ')' format.json {渲染:JSON => @user}' – MID

+0

如果用户&& user.authenticate(PARAMS [:用户] [:密码]) format.json {render:json => @user} end –

+0

'Template is missing',without respond_to do。我无法理解为什么,当我检查唯一性时,它不想要模板。 – MID

0

密码不存储在纯文本格式这是一个盐腌哈希,所以用户无法发现使用的密码。

您可以验证密码是否有效 所以盐腌哈希工作像

  • 加密后的字符串=明文(口令)+哈希算法中(加密)

  • 一些随机字符串(盐)+加密后的字符串,然后检查是否匹配 您可以使用warden.authenticateauthenticate(检查文档)方法对用户进行验证密码

+0

谢谢,但是您知道Devise gem中有用吗? – MID

+0

你想要什么设计你必须定制设备,你可以使用像'resource = warden.authenticate!(:scope => resource_name,:recall =>“home#index”)#覆盖 – Amar

+0

我想我很接近到答案。你能看看上面的评论吗? – MID

5

我使用的轨道4版,当我试图做user.authenticate我得到了错误,说知晓的方法,所以我找到了检查,如果密码是有效的另一种方法,

valid_password?(current_password)

我想分享它所以它可能也会帮助其他人。

相关问题