<commit_before># frozen_string_literal: true
module Pragma
  module Devise
    module Token
      module Operation
        class Create < Pragma::Operation::Base
          include Pragma::Devise::Operation::Defaults

          class << self
            def contract_klass
              super || Pragma::Devise::Token::Contract::Create
            end
          end

          def call
            validate! OpenStruct.new

            context.user = find_user_for_authentication

            unless context.user.active_for_authentication?
              respond_with!(
                status: :unauthorized,
                resource: {
                  error_type: context.user.inactive_message,
                  error_message: I18n.t(
                    "devise.failure.#{context.user.inactive_message}",
                    default: 'You cannot authenticate at this moment.'
                  )
                }
              )
            end

            context.token = <PASSWORD>::AuthToken.new payload: { sub: context.user.id }
            respond_with status: :created, resource: { token: context.token.token }
          end

          protected

          def find_user_for_authentication
            self.class.model_klass.find_for_authentication(email: params[:email]).tap do |user|
              unless user && user.valid_password?(params[:password])
                respond_with!(
                  status: :unprocessable_entity,
                  resource: {
                    error_type: :invalid_credentials,
                    error_message: 'The credentials you have provided are not valid.'
                  }
                )
              end
            end
          end
        end
      end
    end
  end
end<commit_msg>Split user retrieval from response on sign-in<commit_after># frozen_string_literal: true
module Pragma
  module Devise
    module Token
      module Operation
        class Create < Pragma::Operation::Base
          include Pragma::Devise::Operation::Defaults

          class << self
            def contract_klass
              super || Pragma::Devise::Token::Contract::Create
            end
          end

          def call
            validate! OpenStruct.new

            context.user = find_user_for_authentication

            unless context.user
              respond_with!(
                status: :unprocessable_entity,
                resource: {
                  error_type: :invalid_credentials,
                  error_message: 'The credentials you have provided are not valid.'
                }
              )
            end

            unless context.user.active_for_authentication?
              respond_with!(
                status: :unauthorized,
                resource: {
                  error_type: context.user.inactive_message,
                  error_message: I18n.t(
                    "devise.failure.#{context.user.inactive_message}",
                    default: 'You cannot authenticate at this moment.'
                  )
                }
              )
            end

            context.token = Kn<PASSWORD>::AuthToken.new payload: { sub: context.user.id }
            respond_with status: :created, resource: { token: context.token.token }
          end

          protected

          def find_user_for_authentication
            user = self.class.model_klass.find_for_authentication(email: params[:email])
            user if user && user.valid_password?(params[:password])
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07076139258420605, 'fraction_numerical': 0.0011321822813472968, 'mean_word_length': 1.469601677148847, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3222082', 'n_tokens_mistral': 866, 'n_tokens_neox': 822, 'n_words': 196}