Guest User

Untitled

a guest
Apr 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. Warden::Strategies.add(:bcrypt) do
  2. def valid?
  3. params[:username] || params[:password]
  4. end
  5.  
  6. def authenticate!
  7. return fail! unless user = User.first(:username => params[:username])
  8.  
  9. if user.encrypted_password == params[:password]
  10. success!(user)
  11. else
  12. errors.add(:login, "Username or Password incorrect")
  13. fail!
  14. end
  15. end
  16. end
  17.  
  18.  
  19. ############ The user model
  20.  
  21. class User < ActiveRecord::Base
  22. attr_accessor :password, :password_confirmation
  23.  
  24. validates_present :encrypted_password
  25. validates_confirmation_of :password, :if => :password
  26.  
  27. def password=(pass)
  28. @password = pass
  29. self.encrypted_password = pass.nil? ? nil : ::BCrypt::Password.create(pass)
  30. end
  31.  
  32. def encrypted_password
  33. @encrypted_password ||= begin
  34. ep = read_attribute(encrypted_password)
  35. ep.nil? ? nil : ::BCrypt::Password.new(ep)
  36. end
  37. end
  38. end
  39.  
  40.  
  41. ################ DM
  42. class User
  43. attr_accessor :password, :password_confirmation
  44.  
  45. include DataMapper::Resource
  46.  
  47. property :id, Serial
  48. property :encrypted_password, BCryptHash, :nullable => false
  49.  
  50. validates_is_confirmed :password
  51.  
  52. def password=(pass)
  53. @password = pass
  54. self.encrypted_password = pass
  55. end
  56. end
Add Comment
Please, Sign In to add comment