Sha256: 508a07845f30ef8f41c61a4f68180aeeca725b30dea37ab3e3d6595bd1a36ce8

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# frozen-string-literal: true

module Rodauth
  ChangePassword = Feature.define(:change_password) do
    depends :login_password_requirements_base

    notice_flash 'Your password has been changed'
    error_flash 'There was an error changing your password'
    view 'change-password', 'Change Password'
    after
    before
    additional_form_tags
    button 'Change Password'
    redirect

    auth_value_method :new_password_label, 'New Password'
    auth_value_method :new_password_param, 'new-password'

    auth_value_methods :change_password_requires_password?

    route do |r|
      require_account
      before_change_password_route

      r.get do
        change_password_view
      end

      r.post do
        catch_error do
          if change_password_requires_password? && !password_match?(param(password_param))
            throw_error_status(invalid_password_error_status, password_param, invalid_password_message)
          end

          password = param(new_password_param)
          if require_password_confirmation? && password != param(password_confirm_param)
            throw_error_status(unmatched_field_error_status, new_password_param, passwords_do_not_match_message)
          end

          if password_match?(password) 
            throw_error_status(invalid_field_error_status, new_password_param, same_as_existing_password_message)
          end

          unless password_meets_requirements?(password)
            throw_error_status(invalid_field_error_status, new_password_param, password_does_not_meet_requirements_message)
          end

          transaction do
            before_change_password
            set_password(password)
            after_change_password
          end
          set_notice_flash change_password_notice_flash
          redirect change_password_redirect
        end

        set_error_flash change_password_error_flash
        change_password_view
      end
    end

    def change_password_requires_password?
      modifications_require_password?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rodauth-1.8.0 lib/rodauth/features/change_password.rb