Skip to content

Instantly share code, notes, and snippets.

@thomasbiddle
Created May 4, 2013 22:53
Show Gist options
  • Save thomasbiddle/5519046 to your computer and use it in GitHub Desktop.
Save thomasbiddle/5519046 to your computer and use it in GitHub Desktop.
class Users < ActiveRecord::Base
attr_accessible(
:nickname,
:email
)
# Set password to accessor so we can validate it below.
attr_accessor(
:password
)
validates(:email, :uniqueness => true, :presence => true)
validates(:nickname, :presence => true)
validates(:password, :length => { :minimum => 8 })
def authenticate(password)
self.hash_password(params[:password]) == self.password_hash
end
def save
self.salt = self.salt ||= SCrypt::Engine.generate_salt(:max_time => 1, :salt_size => 32)
self.password_hash = hash_password(self.password, salt)
super
end
private
def hash_password(password, salt)
SCrypt::Engine.hash_secret(password, salt)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment