Class: CommandTower::Services::Auth::PasswordReset::Reset

Inherits:
CommandTower::Services::ApplicationService show all
Includes:
TokenVerification
Defined in:
app/services/command_tower/services/auth/password_reset/reset.rb

Overview

Consumes a reset token and replaces the account password.

Constant Summary collapse

RESET_MESSAGE =
"Password has been successfully reset"
CONFIRMATION_MISMATCH =
"Password and confirmation do not match"

Constants inherited from CommandTower::ServiceBase

CommandTower::ServiceBase::ON_ARGUMENT_VALIDATION

Instance Method Summary collapse

Methods inherited from CommandTower::Services::ApplicationService

call, inherited

Methods included from Transactional

#fail_transaction!, #transaction

Methods inherited from CommandTower::ServiceBase

#command_tower_lifecycle, inherited, #internal_validate, #service_lifecycle_error_codes, #service_lifecycle_log_level, #validate!

Methods included from Logging::LifecycleDeclaration

included

Methods included from Execution::ContextAccess

#audit, #execution_context, #log_debug, #log_error, #log_info, #log_warn, #publish_event

Methods included from ArgumentValidation

included

Methods included from CommandTower::ServiceLogging

included

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/command_tower/services/auth/password_reset/reset.rb', line 19

def call
  reject!(password_confirmation: CONFIRMATION_MISMATCH) unless password == password_confirmation
  reject!(password: password_length_message) unless password_length_valid?

  require_email!

  redemption = redeem_token!(access_count: true)
  user = redemption.user
  match_email!(token_user: user)

  user.password = password
  user.password_confirmation = password_confirmation

  ActiveRecord::Base.transaction do
    unless user.save
      log_error("Failed to update the password for user [#{user.id}]")
      reject!(user.errors.to_hash.transform_values { Array(_1).join(", ") })
    end

    audit(
      :password_changed,
      subject: user,
      affected_user: user,
      changes: {},
      metadata: { mechanism: "reset" }
    )
  end

  log_info("Password reset completed for user [#{user.id}]")
  context.message = RESET_MESSAGE
end