Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'bcrypt'
- module Ark
- module Mixins
- module BCryptUser
- def self.included(base)
- base.class_eval do
- attr_accessor :password, :password_confirmation
- before_save :encrypt_password
- field :crypted_password, :type => String, :length => 60
- validates_presence_of :password, :password_confirmation, :if => proc { self.password_required? }
- validates_confirmation_of :password, :if => proc { self.password_required? }
- extend Ark::Mixins::BCryptUser::ClassMethods
- include Ark::Mixins::BCryptUser::InstanceMethods
- end
- end
- module ClassMethods
- def authenticate(username, password)
- puts username
- puts password
- u = User.find(username)
- u && u.authenticated?(password) ? u : nil
- end
- end
- module InstanceMethods
- def authenticated?(password)
- bcrypt_password == password
- end
- def bcrypt_password
- @bcrypt_password ||= BCrypt::Password.new(crypted_password)
- end
- def password_required?
- crypted_password.blank? || !password.blank?
- end
- def encrypt_password
- return if password.blank?
- cost = BCrypt::Engine::DEFAULT_COST
- self.crypted_password = BCrypt::Password.create(password, :cost => cost)
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment