Created
January 19, 2012 03:58
-
-
Save eagsalazar/1637700 to your computer and use it in GitHub Desktop.
Migrating from Devise to Authentication from Scratch ala Railscasts #250
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
field :encrypted_password, :type => String | |
field :email, :type => String | |
attr_accessor :password | |
validates_confirmation_of :password | |
validates_presence_of :password, :on => :create, :message => "can't be blank" | |
validates_presence_of :email, :message => "can't be blank" | |
validates_uniqueness_of :email, :message => "already in use" | |
before_create :encrypt_password | |
# Password and auth stuff. | |
def saved_password | |
@saved_password ||= BCrypt::Password.new(encrypted_password) | |
end | |
def saved_password=(new_password) | |
@saved_password = BCrypt::Password.create(new_password) | |
self.encrypted_password = @saved_password | |
end | |
def encrypt_password | |
saved_password = password if password.present? | |
end | |
def self.authenticate email, password | |
user = where(:email => email).first | |
if user && user.saved_password == password | |
user | |
else | |
nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment