Create a gist now

Instantly share code, notes, and snippets.

Warden::Strategies.add(:bcrypt) do
def valid?
params[:username] || params[:password]
end
def authenticate!
return fail! unless user = User.first(:username => params[:username])
if user.encrypted_password == params[:password]
success!(user)
else
errors.add(:login, "Username or Password incorrect")
fail!
end
end
end
############ The user model
class User < ActiveRecord::Base
attr_accessor :password, :password_confirmation
validates_present :encrypted_password
validates_confirmation_of :password, :if => :password
def password=(pass)
@password = pass
self.encrypted_password = pass.nil? ? nil : ::BCrypt::Password.create(pass)
end
def encrypted_password
@encrypted_password ||= begin
ep = read_attribute(encrypted_password)
ep.nil? ? nil : ::BCrypt::Password.new(ep)
end
end
end
################ DM
class User
attr_accessor :password, :password_confirmation
include DataMapper::Resource
property :id, Serial
property :encrypted_password, BCryptHash, :nullable => false
validates_is_confirmed :password
def password=(pass)
@password = pass
self.encrypted_password = pass
end
end
@rdmcfee
rdmcfee commented Sep 9, 2014

Hi Daniel,

In your activerecord example within the encrypted password block, did you mean to pass the symbol :encypted_password into the read_attribute call instead of passing the method itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment