Rails 7 验证返回“无效哈希”
- BCrypt 处于活动状态
- 模型:has_secure_password
- Rails 7.0.2.3
- Ubuntu 20.04 通过 WSL2 Windows 10
我可以创建,但无法验证密码。 当在 Rails 控制台中尝试以下操作时,我最终得到一个无效的哈希值(最后一行):
irb(main):001:0> admin = Admin.new(username: 'screenbeam', email: '[email protected]', password_digest: 'password')
=> #<Admin:0x000055c2cc1fd080 id: nil, username: "screenbeam", email: "[email protected]", password_digest: "[FILTERED]", created_at: nil, updated_at: nil>
irb(main):002:0> admin.save
TRANSACTION (0.3ms) BEGIN
Admin Exists? (0.7ms) SELECT 1 AS one FROM "admins" WHERE LOWER("admins"."username") = LOWER($1) LIMIT $2 [["username", "screenbeam"], ["LIMIT", 1]]
Admin Create (0.5ms) INSERT INTO "admins" ("username", "email", "password_digest", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["username", "screenbeam"], ["email", "[email protected]"], ["password_digest", "[FILTERED]"], ["created_at", "2022-03-25 10:54:35.072350"], ["updated_at", "2022-03-25 10:54:35.072350"]]
TRANSACTION (9.8ms) COMMIT
=> true
irb(main):003:0> admin.authenticate('password')
/home/michael/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.17/lib/bcrypt/password.rb:60:in `initialize': invalid hash (BCrypt::Errors::InvalidHash)
irb(main):004:0>
- BCrypt is active
- Model : has_secure_password
- Rails 7.0.2.3
- Ubuntu 20.04 via WSL2 Windows 10
I can create, but I can't authenticate the passwords.
When trying this following in the Rails Console I end up with an invalid hash (last line):
irb(main):001:0> admin = Admin.new(username: 'screenbeam', email: '[email protected]', password_digest: 'password')
=> #<Admin:0x000055c2cc1fd080 id: nil, username: "screenbeam", email: "[email protected]", password_digest: "[FILTERED]", created_at: nil, updated_at: nil>
irb(main):002:0> admin.save
TRANSACTION (0.3ms) BEGIN
Admin Exists? (0.7ms) SELECT 1 AS one FROM "admins" WHERE LOWER("admins"."username") = LOWER($1) LIMIT $2 [["username", "screenbeam"], ["LIMIT", 1]]
Admin Create (0.5ms) INSERT INTO "admins" ("username", "email", "password_digest", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["username", "screenbeam"], ["email", "[email protected]"], ["password_digest", "[FILTERED]"], ["created_at", "2022-03-25 10:54:35.072350"], ["updated_at", "2022-03-25 10:54:35.072350"]]
TRANSACTION (9.8ms) COMMIT
=> true
irb(main):003:0> admin.authenticate('password')
/home/michael/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/bcrypt-3.1.17/lib/bcrypt/password.rb:60:in `initialize': invalid hash (BCrypt::Errors::InvalidHash)
irb(main):004:0>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试直接设置哈希...
需要让
has_secure_password
做它的事情,并且只需在创建用户时设置密码...You tried to set the hash directly...
Need to let
has_secure_password
do its thing, and just set the password when creating the user...