module ActiveModel::SecurePassword::InstanceMethodsOnActivation

Attributes

password[R]

Public Instance Methods

authenticate(unencrypted_password) Show source
# File activemodel/lib/active_model/secure_password.rb, line 101
def authenticate(unencrypted_password)
  BCrypt::Password.new(password_digest) == unencrypted_password && self
end

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
password=(unencrypted_password) Show source
# File activemodel/lib/active_model/secure_password.rb, line 119
def password=(unencrypted_password)
  if unencrypted_password.nil?
    self.password_digest = nil
  elsif !unencrypted_password.empty?
    @password = unencrypted_password
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
    self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost)
  end
end

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

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."
password_confirmation=(unencrypted_password) Show source
# File activemodel/lib/active_model/secure_password.rb, line 129
def password_confirmation=(unencrypted_password)
  @password_confirmation = unencrypted_password
end

© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.