You need to add the attr_encryptor
line after you have specified the Data Mapper properties. This prevents DataMapper simply replacing the encrypted_password
accessors with its own:
class User
include DataMapper::Resource
property :id, Serial
property :encrypted_password, Text
# this line moved down from above
attr_encryptor :password, :key => 'secret key'
end
and then create the user with:
user = User.new(:username => username, :password => password)
Are you sure you want to search for a User based on an encrypted password? Normally you would find the user based on e.g. username and then just check the password matches.
If you do want to do this, you will have to either recreate the encrypted password in your code and search with that (you’ll need to check the docs to see how encryption is done):
User.all(:username => username, :encrypted_password => encrypt(password))
Alternatively fetch all matching users and filter them in your code:
User.all(:username => name).select {|u| u.password == password}