module Negroni::Models::Recoverable
Recoverable
takes care of resetting the user password and send reset instructions.
## Required attributes
Recoverable
requires that the including class has the following attributes:
* `reset_password_sent_at`: [DateTime] * `reset_password_token`: [String]
## Options
Recoverable
adds the following class options:
* `reset_password_keys`: the keys you want to use when recovering the password for an account * `reset_password_within`: the time period within which the password must be reset or the token expires. * `sign_in_after_reset_password`: whether or not to sign in the user automatically after a password reset.
@example
# resets the user password and save the record, true if valid passwords # are given, otherwise false User.find(1).reset_password('password123', 'password123')
@example
# creates a new token and send it with instructions about how to reset # the password User.find(1).send_reset_password_instructions
Public Class Methods
Required fields for Recoverable
# File lib/negroni/models/recoverable.rb, line 40 def self.required_fields(_klass = nil) [:reset_password_sent_at, :reset_password_token] end
Public Instance Methods
Update password saving the record and clearing token. Returns true if the passwords are valid and the record was saved, false otherwise.
@param new_password [String] the new password @param new_password_confirmation [String] confirmation of the password
@return [Boolean] if the operation was successful
# File lib/negroni/models/recoverable.rb, line 56 def reset_password(new_password, new_password_confirmation) self.password = new_password self.password_confirmation = new_password_confirmation save end
Checks if the reset password token sent is within the limit time. We do this by calculating if the difference between today and the sending date does not exceed the confirm in time configured. Returns true if the resource is not responding to reset_password_sent_at at all.
@return [Boolean] if the period is valid.
reset_password_within is a model configuration, must always be an integer value.
@example
# reset_password_within = 1.day and reset_password_sent_at = today reset_password_period_valid? # => true
@example
# reset_password_within = 5.days; reset_password_sent_at = 4.days.ago reset_password_period_valid? # => true
@example
# reset_password_within = 5.days; reset_password_sent_at = 5.days.ago reset_password_period_valid? # => false
@example
# reset_password_within = 0.days reset_password_period_valid? # will always return false
# File lib/negroni/models/recoverable.rb, line 100 def reset_password_period_valid? reset_password_sent_at && reset_password_sent_at.utc >= self.class.reset_password_within.ago.utc end
Resets reset password token and send reset password instructions by email. Returns the token sent in the e-mail.
@return [String] the token that was sent in the email.
# File lib/negroni/models/recoverable.rb, line 67 def send_reset_password_instructions token = set_reset_password_token send_reset_password_instructions_notification(token) token end
Protected Instance Methods
Removes `reset_password_token`
# File lib/negroni/models/recoverable.rb, line 108 def clear_reset_password_token self.reset_password_token = nil self.reset_password_sent_at = nil end
# File lib/negroni/models/recoverable.rb, line 129 def clear_reset_password_token? auth_keys_changed = self.class.authentication_keys.any? do |attribute| respond_to?("#{attribute}_changed?") && send("#{attribute}_changed?") end !new_record? && (auth_keys_changed || password_digest_changed?) end
# File lib/negroni/models/recoverable.rb, line 125 def send_reset_password_instructions_notification(token) send_auth_notification(:reset_password_instructions, token, {}) end
Sets the `reset_password_token` for the record
# File lib/negroni/models/recoverable.rb, line 114 def set_reset_password_token raw, encoded = Negroni.token_generator.generate(self.class, :reset_password_token) self.reset_password_token = encoded self.reset_password_sent_at = Time.now.utc save(validate: false) # rubocop:disable Rails/SaveBang raw end