Module: HasSecurePasswordArgon2::InstanceMethodsOnActivation

Defined in:
lib/has_secure_password_argon2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



50
51
52
# File 'lib/has_secure_password_argon2.rb', line 50

def password
  @password
end

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


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/has_secure_password_argon2.rb', line 38

def authenticate(unencrypted_password)
  if password_digest.start_with?('$argon2')
    Argon2::Password.verify_password(unencrypted_password, password_digest, HasSecurePasswordArgon2.secret) && self
  elsif super(unencrypted_password)
    self.password = hash_argon2_password(unencrypted_password)
    save(validate: false)
    self
  else
    false
  end
end