簡體   English   中英

嘗試驗證密碼時出現“ BCrypt :: Errors :: InvalidHash:無效哈希”

[英]“BCrypt::Errors::InvalidHash: invalid hash” when trying to authenticate password

我有一個模型User

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'

我什至不認為該參數應該是哈希值:難道它不應該只是一個字符串嗎? 這里發生了什么?

它不是Hash對象,它引用的password_digest哈希值似乎無效,也就是說,您設置不正確。

嘗試這個:

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

現在,您可以通過以下方式進行身份驗證:

user.authenticate('password')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM