Guest User

Untitled

a guest
Sep 4th, 2018
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1.  
  2. field :encrypted_password, :type => String
  3. field :email, :type => String
  4.  
  5. attr_accessor :password
  6. validates_confirmation_of :password
  7. validates_presence_of :password, :on => :create, :message => "can't be blank"
  8. validates_presence_of :email, :message => "can't be blank"
  9. validates_uniqueness_of :email, :message => "already in use"
  10.  
  11. before_create :encrypt_password
  12.  
  13. # Password and auth stuff.
  14. def saved_password
  15. @saved_password ||= BCrypt::Password.new(encrypted_password)
  16. end
  17. def saved_password=(new_password)
  18. @saved_password = BCrypt::Password.create(new_password)
  19. self.encrypted_password = @saved_password
  20. end
  21. def encrypt_password
  22. saved_password = password if password.present?
  23. end
  24. def self.authenticate email, password
  25. user = where(:email => email).first
  26. if user && user.saved_password == password
  27. user
  28. else
  29. nil
  30. end
  31. end
Add Comment
Please, Sign In to add comment