Module: Veri::Authenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/veri/models/concerns/authenticatable.rb

Instance Method Summary collapse

Instance Method Details

#lock!Object



41
42
43
44
45
46
# File 'lib/veri/models/concerns/authenticatable.rb', line 41

def lock!
  transaction do
    update!(locked: true, locked_at: Time.current)
    sessions.terminate_all
  end
end

#unlock!Object



48
49
50
# File 'lib/veri/models/concerns/authenticatable.rb', line 48

def unlock!
  update!(locked: false, locked_at: nil)
end

#update_password(password) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/veri/models/concerns/authenticatable.rb', line 16

def update_password(password)
  update!(
    hashed_password: hasher.create(
      Veri::Inputs::NonEmptyString.new(password, message: "Expected a non-empty string, got `#{password.inspect}`").process
    ),
    password_updated_at: Time.current
  )
end

#verify_password(password) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/veri/models/concerns/authenticatable.rb', line 25

def verify_password(password)
  processed_password = Veri::Inputs::NonEmptyString.new(password, message: "Expected a non-empty string, got `#{password.inspect}`").process

  return false if hashed_password.blank?

  stored_hasher = Veri::Configuration::HASHERS.values.find { _1.match?(hashed_password) }

  raise Veri::Error, "Unrecognized password hash format" unless stored_hasher

  return false unless stored_hasher.verify(processed_password, hashed_password)

  update_column(:hashed_password, hasher.create(processed_password)) unless stored_hasher == hasher

  true
end