Module: ActiveRecord::SecurePassword::InstanceMethodsOnActivation

Defined in:
lib/active_record/secure_password.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(unencrypted_password) ⇒ Object

Returns self if the password is correct, otherwise false.

class User < ActiveRecord::Base
  has_secure_password validations: false
end

user = User.new(name: 'david', password: 'mUc3m00RsqyRe')
user.save
user.authenticate('notright')      # => false
user.authenticate('mUc3m00RsqyRe') # => user


88
89
90
# File 'lib/active_record/secure_password.rb', line 88

def authenticate(unencrypted_password)
  BCrypt::Password.new(password_digest) == unencrypted_password && self
end

#password=(unencrypted_password) ⇒ Object

Encrypts the password into the password_digest attribute, only if the new password is not blank.

class User < ActiveRecord::Base
  has_secure_password validations: false
end

user = User.new
user.password = nil
user.password_digest # => nil
user.password = 'mUc3m00RsqyRe'
user.password_digest # => "$2a$10$4LEA7r4YmNHtvlAvHhsYAeZmk/xeUVtMTYqwIvYY76EW5GUqDiP4."


104
105
106
107
108
109
110
# File 'lib/active_record/secure_password.rb', line 104

def password=(unencrypted_password)
  unless unencrypted_password.blank?
    @password = unencrypted_password
    cost = ActiveRecord::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine::DEFAULT_COST
    self.password_digest = BCrypt::Password.create(unencrypted_password, :cost => cost)
  end
end

#password_confirmation=(unencrypted_password) ⇒ Object



112
113
114
# File 'lib/active_record/secure_password.rb', line 112

def password_confirmation=(unencrypted_password)
  @password_confirmation = unencrypted_password
end