Rails 7 认证 returns 'invalid hash'

Rails 7 authenticate returns 'invalid hash'

我可以创建,但我无法验证密码。 在 Rails 控制台中尝试以下操作时,我得到了一个无效的散列(最后一行):

irb(main):001:0> admin = Admin.new(username: 'screenbeam', email: 'screenbeam@screenbeam.com', password_digest: 'password')
=> #<Admin:0x000055c2cc1fd080 id: nil, username: "screenbeam", email: "screenbeam@screenbeam.com", 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() LIMIT   [["username", "screenbeam"], ["LIMIT", 1]]
  Admin Create (0.5ms)  INSERT INTO "admins" ("username", "email", "password_digest", "created_at", "updated_at") VALUES (, , , , ) RETURNING "id"  [["username", "screenbeam"], ["email", "screenbeam@screenbeam.com"], ["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>

您尝试直接设置哈希...

password_digest: 'password'

需要让has_secure_password做它的事情,创建用户时设置密码即可...

Admin.new(username: 'screenbeam', email: 'screenbeam@screenbeam.com', password: 'password')