0
雖然我不想在我的數據庫中有密碼列,但我試圖驗證「密碼」的存在......我將密碼轉換爲密碼鹽。我如何驗證密碼的存在性,然後在沒有密碼列的情況下保存加密?驗證密碼的存在,但沒有在續集中的密碼列
架構:
migration "create users table" do
database.create_table :users do
primary_key :id
text :name
text :email
text :username
text :password_hash
text :password_salt
index :username, :unique => true
end
end
用戶模型:
class User < Sequel::Model
require 'bcrypt'
def before_save
if password
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
def validate
super
validates_presence [:password,:email] if new?
validates_unique [:email]
end
def self.authenticate(email, password)
user = User.find(:email=>email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
當我嘗試驗證密碼的情況下,我得到
NoMethodError: undefined method `password' for #<User @values={:email=>"[email protected]"}>