Sha256: bce62742b407782348cc09fd76a19a8bf04276b52380a8e9a81649d557ced095

Contents?: true

Size: 1.58 KB

Versions: 16

Compression:

Stored size: 1.58 KB

Contents

module GraphqlDevise
  module Mutations
    class UpdatePassword < Base
      argument :password,              String, required: true
      argument :password_confirmation, String, required: true
      argument :current_password,      String, required: false

      def resolve(current_password: nil, **attrs)
        if current_resource.blank?
          raise_user_error(I18n.t('graphql_devise.not_authenticated'))
        elsif current_resource.provider != 'email'
          raise_user_error(
            I18n.t('graphql_devise.passwords.password_not_required', provider: current_resource.provider.humanize)
          )
        end

        if update_resource_password(current_password, attrs)
          current_resource.allow_password_change = false if recoverable_enabled?
          current_resource.save!

          yield current_resource if block_given?

          { authenticatable: current_resource }
        else
          raise_user_error_list(
            I18n.t('graphql_devise.passwords.update_password_error'),
            errors: current_resource.errors.full_messages
          )
        end
      end

      private

      def update_resource_password(current_password, attrs)
        allow_password_change = recoverable_enabled? && current_resource.allow_password_change == true
        if DeviseTokenAuth.check_current_password_before_update == false || allow_password_change
          current_resource.public_send(:update, attrs)
        else
          current_resource.public_send(:update_with_password, attrs.merge(current_password: current_password))
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
graphql_devise-0.12.3 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.12.2 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.12.1 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.12.0 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.11.4 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.11.3 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.11.2 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.11.1 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.11.0 lib/graphql_devise/mutations/update_password.rb
graphql_devise-0.10.1 app/graphql/graphql_devise/mutations/update_password.rb
graphql_devise-0.10.0 app/graphql/graphql_devise/mutations/update_password.rb
graphql_devise-0.9.2 app/graphql/graphql_devise/mutations/update_password.rb
graphql_devise-0.9.1 app/graphql/graphql_devise/mutations/update_password.rb
graphql_devise-0.9.0 app/graphql/graphql_devise/mutations/update_password.rb
graphql_devise-0.8.1 app/graphql/graphql_devise/mutations/update_password.rb
graphql_devise-0.8.0 app/graphql/graphql_devise/mutations/update_password.rb