Created
May 1, 2011 09:51
-
-
Save eteubert/950378 to your computer and use it in GitHub Desktop.
A simple user model with and without `has_secure_password` helper.
This file contains 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
class User < ActiveRecord::Base | |
has_secure_password | |
attr_accessible :email, :password, :password_confirmation | |
validates_presence_of :email | |
validates_uniqueness_of :email | |
def self.authenticate(email, password) | |
find_by_email(email).try(:authenticate, password) | |
end | |
end |
This file contains 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
class User < ActiveRecord::Base | |
attr_accessible :email, :password, :password_confirmation | |
attr_accessor :password | |
before_save :encrypt_password | |
validates_confirmation_of :password | |
validates_presence_of :password, :on => :create | |
validates_presence_of :email | |
validates_uniqueness_of :email | |
def self.authenticate(email, password) | |
user = find_by_email(email) | |
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) | |
user | |
else | |
nil | |
end | |
end | |
def encrypt_password | |
if password.present? | |
self.password_salt = BCrypt::Engine.generate_salt | |
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment