Class: Lines::User
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Lines::User
- Defined in:
- app/models/lines/user.rb
Instance Attribute Summary collapse
-
#reset_token ⇒ Object
Returns the value of attribute reset_token.
Class Method Summary collapse
-
.digest(string) ⇒ Object
Returns the hash digest of the given string.
-
.generate_token ⇒ Object
Generate a random token.
Instance Method Summary collapse
-
#authenticated?(attribute, token) ⇒ Boolean
Returns true if the given token matches the digest.
-
#create_reset_digest ⇒ Object
Sets
rest_digest
andreset_sent_at
for password reset. -
#password_reset_expired? ⇒ Boolean
Returns true if a password reset has expired.
-
#send_password_reset_email ⇒ Object
Sends email with instructions how to reset password.
Instance Attribute Details
#reset_token ⇒ Object
Returns the value of attribute reset_token.
21 22 23 |
# File 'app/models/lines/user.rb', line 21 def reset_token @reset_token end |
Class Method Details
.digest(string) ⇒ Object
Returns the hash digest of the given string.
54 55 56 57 |
# File 'app/models/lines/user.rb', line 54 def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end |
.generate_token ⇒ Object
Generate a random token.
49 50 51 |
# File 'app/models/lines/user.rb', line 49 def User.generate_token SecureRandom.urlsafe_base64 end |
Instance Method Details
#authenticated?(attribute, token) ⇒ Boolean
Returns true if the given token matches the digest.
41 42 43 44 45 |
# File 'app/models/lines/user.rb', line 41 def authenticated?(attribute, token) digest = send("#{attribute}_digest") return false if digest.nil? BCrypt::Password.new(digest).is_password?(token) end |
#create_reset_digest ⇒ Object
Sets rest_digest
and reset_sent_at
for password reset.
29 30 31 32 33 |
# File 'app/models/lines/user.rb', line 29 def create_reset_digest self.reset_token = Lines::User.generate_token update_attribute(:reset_digest, Lines::User.digest(reset_token)) update_attribute(:reset_sent_at, Time.zone.now) end |
#password_reset_expired? ⇒ Boolean
Returns true if a password reset has expired.
60 61 62 |
# File 'app/models/lines/user.rb', line 60 def password_reset_expired? reset_sent_at < 2.hours.ago end |
#send_password_reset_email ⇒ Object
Sends email with instructions how to reset password.
36 37 38 |
# File 'app/models/lines/user.rb', line 36 def send_password_reset_email UserMailer.password_reset(self).deliver_now end |