Created
June 17, 2009 02:56
-
-
Save hassox/131050 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?