Module: HasEditablePassword

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::SecurePassword
Defined in:
lib/has_editable_password.rb

Instance Method Summary collapse

Instance Method Details

#current_password_match?Boolean

Returns true if current_password matches the stored password_digest.

Returns:

  • (Boolean)


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(options = {})
  token = SecureRandom.urlsafe_base64(options.delete(:length) || 32)
  self.password_recovery_token = BCrypt::Password.create(token)
  self.password_recovery_token_creation = Time.now
  save unless options.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

Returns:

  • (Boolean)


57
58
59
# File 'lib/has_editable_password.rb', line 57

def valid_recovery_token?
  recovery_token_match? and !recovery_token_expired?
end