Skip to content

Instantly share code, notes, and snippets.

@jamiecook
Created April 6, 2012 07:02
Show Gist options
  • Save jamiecook/2317835 to your computer and use it in GitHub Desktop.
Save jamiecook/2317835 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
# new columns need to be added here to be writable through mass assignment
attr_accessible :email, :password, :password_confirmation # :username,
attr_accessor :password, :password_salt
before_save :prepare_password
#validates_presence_of :username
#validates_uniqueness_of :username, :email, :allow_blank => true
validates_uniqueness_of :email, :allow_blank => false
#validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_presence_of :password, :on => :create
validates_confirmation_of :password
validates_length_of :password, :minimum => 4, :allow_blank => true
# login can be either username or email address
def self.authenticate(login, pass)
user = find_by_email(login)
return user if user && user.matching_password?(pass)
end
def matching_password?(pass)
puts "self.inspect : #{self.inspect}"
#puts "line 24: " + self.password
self.password == encrypt_password(pass)
end
private
def prepare_password
unless password.blank?
self.password_salt = BCrypt::Engine.generate_salt
self.password = encrypt_password(password)
end
end
def encrypt_password(pass)
puts "jamie town"
puts pass
puts self.password_salt
BCrypt::Engine.hash_secret(pass, @password_salt)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment