Module: BetterAuthy::Models::Authenticable

Extended by:
ActiveSupport::Concern
Defined in:
lib/better_authy/models/authenticable.rb

Instance Method Summary collapse

Instance Method Details

#authenticable_scope ⇒ Object

Returns the scope name for this model



28
29
30
# File 'lib/better_authy/models/authenticable.rb', line 28

def authenticable_scope
  self.class.authenticable_scope_name
end

#authenticable_scope_config ⇒ Object

Returns the scope configuration



33
34
35
# File 'lib/better_authy/models/authenticable.rb', line 33

def authenticable_scope_config
  BetterAuthy.scope_for(authenticable_scope)
end

#clear_password_reset_token! ⇒ Object

Clear password reset token



96
97
98
99
100
101
# File 'lib/better_authy/models/authenticable.rb', line 96

def clear_password_reset_token!
  update!(
    password_reset_token_digest: nil,
    password_reset_sent_at: nil
  )
end

#forget_me! ⇒ Object

Clear remember token



48
49
50
51
52
53
# File 'lib/better_authy/models/authenticable.rb', line 48

def forget_me!
  update!(
    remember_token_digest: nil,
    remember_created_at: nil
  )
end

#generate_password_reset_token! ⇒ Object

Generate password reset token



77
78
79
80
81
82
83
84
# File 'lib/better_authy/models/authenticable.rb', line 77

def generate_password_reset_token!
  token = SecureRandom.urlsafe_base64(32)
  update!(
    password_reset_token_digest: BCrypt::Password.create(token),
    password_reset_sent_at: Time.current
  )
  token
end

#password_reset_token_valid?(token) ⇒ Boolean

Validate password reset token

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/better_authy/models/authenticable.rb', line 87

def password_reset_token_valid?(token)
  return false if password_reset_token_digest.blank?
  return false if password_reset_sent_at.blank?
  return false if password_reset_sent_at < authenticable_scope_config.password_reset_within.ago

  BCrypt::Password.new(password_reset_token_digest).is_password?(token)
end

#remember_me! ⇒ Object

Generate remember token



38
39
40
41
42
43
44
45
# File 'lib/better_authy/models/authenticable.rb', line 38

def remember_me!
  token = SecureRandom.urlsafe_base64(32)
  update!(
    remember_token_digest: BCrypt::Password.create(token),
    remember_created_at: Time.current
  )
  token
end

#remember_token_valid?(token) ⇒ Boolean

Validate remember token

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/better_authy/models/authenticable.rb', line 56

def remember_token_valid?(token)
  return false if remember_token_digest.blank?
  return false if remember_created_at.blank?
  return false if remember_created_at < authenticable_scope_config.remember_for.ago

  BCrypt::Password.new(remember_token_digest).is_password?(token)
end

#reset_password!(new_password, new_password_confirmation) ⇒ Object

Reset password with confirmation



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/better_authy/models/authenticable.rb', line 104

def reset_password!(new_password, new_password_confirmation)
  self.password = new_password
  self.password_confirmation = new_password_confirmation

  if valid?
    save!
    clear_password_reset_token!
    true
  else
    false
  end
end

#track_sign_in!(request) ⇒ Object

Track sign in



65
66
67
68
69
70
71
72
73
74
# File 'lib/better_authy/models/authenticable.rb', line 65

def track_sign_in!(request)
  now = Time.current
  update!(
    sign_in_count:  + 1,
    last_sign_in_at: ,
    last_sign_in_ip: ,
    current_sign_in_at: now,
    current_sign_in_ip: request.remote_ip
  )
end