Module: HasEditablePassword
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::SecurePassword
- Defined in:
- lib/has_editable_password.rb
Instance Method Summary collapse
-
#current_password_match? ⇒ Boolean
Returns true if
current_passwordmatches the storedpassword_digest. -
#generate_recovery_token(options = {}) ⇒ Object
Creates a new
password_recovery_token. -
#valid_recovery_token? ⇒ Boolean
Returns true if the
recovery_tokenmatches with the stored one and the token creation time is less than 24 hours ago.
Instance Method Details
#current_password_match? ⇒ Boolean
Returns true if current_password matches the stored password_digest.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/has_editable_password.rb', line 64 def current_password_match? if @current_password if @old_password_digest BCrypt::Password.new(@old_password_digest) == @current_password else # almost same as #authenticate (returns true instead of the object) BCrypt::Password.new(self.password_digest) == @current_password end else false end end |
#generate_recovery_token(options = {}) ⇒ Object
Creates a new password_recovery_token
If a token was already there it is discarded. Also sets password_recovery_token_creation to the current time. Unless specified it calls save to store the token in the database.
options - this is the length of the SecureRandom string generated
as the token. Since the token is base64_encoded it will be longer than
that. Default is 32.
options - you can use this if you don’t want save to be called.
generate_recovery_token(save: false)
45 46 47 48 49 50 51 |
# File 'lib/has_editable_password.rb', line 45 def generate_recovery_token( = {}) token = SecureRandom.urlsafe_base64(.delete(:length) || 32) self.password_recovery_token = BCrypt::Password.create(token) self.password_recovery_token_creation = Time.now save unless .delete(:save) == false token end |
#valid_recovery_token? ⇒ Boolean
Returns true if the recovery_token matches with the stored one and the token creation time is less than 24 hours ago
57 58 59 |
# File 'lib/has_editable_password.rb', line 57 def valid_recovery_token? recovery_token_match? and !recovery_token_expired? end |