2017-05-27 390 views
0

时,我有一个模型User“BCrypt ::错误:: InvalidHash:无效的哈希” 尝试验证密码

class User < ActiveRecord::Base 
    has_secure_password 
end 

我使用gem 'bcrypt', '3.1.11'。但由于某些原因,authenticate方法不起作用。

我有一个用户记录user其中user.password_digest == "password"。但在控制台中输入user.authenticate('password')返回以下错误:

> user.authenticate('password') 
BCrypt::Errors::InvalidHash: invalid hash 
from /home/.rvm/gems/bcrypt-3.1.11/lib/bcrypt/password.rb:60:in 'initialize' 

我甚至不认为该说法应该是一个散列:不只是应该是一个字符串?这里发生了什么?

回答

1

这不是Hash对象,它是指password_digest中的hash value这似乎是无效的,也就是说,您设置不正确。

试试这个:

user.authenticate('password') 

user.password = 'password' 
user.password_confirmation = 'password' 
user.save 

现在你可以验证