<commit_before>module Spree
  class UserSessionsController < Devise::SessionsController
    helper 'spree/base', 'spree/store'

    include Spree::Core::ControllerHelpers::Auth
    include Spree::Core::ControllerHelpers::Common
    include Spree::Core::ControllerHelpers::Order
    include Spree::Core::ControllerHelpers::SSL
    include I18nHelper

    ssl_required :new, :create, :destroy, :update
    ssl_allowed :login_bar

    before_action :set_checkout_redirect, only: :create
    after_action :ensure_valid_locale, only: :create

    def create
      authenticate_spree_user!

      if spree_user_signed_in?
        respond_to do |format|
          format.html {
            flash[:success] = t('devise.success.logged_in_succesfully')
            redirect_back_or_default(after_sign_in_path_for(spree_current_user))
          }
          format.js {
            render json: { email: spree_current_user.login }, status: :ok
          }
        end
      else
        respond_to do |format|
          format.html {
            flash.now[:error] = t('devise.failure.invalid')
            render :new
          }
          format.js {
            render json: { message: t('devise.failure.invalid') }, status: :unauthorized
          }
        end
      end
    end

    private

    def accurate_title
      Spree.t(:login)
    end

    def redirect_back_or_default(default)
      redirect_to(session["spree_user_return_to"] || default)
      session["spree_user_return_to"] = nil
    end

    def ensure_valid_locale
      return unless spree_current_user && !available_locale?(spree_current_user.locale)

      spree_current_user.update!(locale: I18n.default_locale)
    end
  end
end
<commit_msg>Rename method and add explanatory comment<commit_after>module Spree
  class UserSessionsController < Devise::SessionsController
    helper 'spree/base', 'spree/store'

    include Spree::Core::ControllerHelpers::Auth
    include Spree::Core::ControllerHelpers::Common
    include Spree::Core::ControllerHelpers::Order
    include Spree::Core::ControllerHelpers::SSL
    include I18nHelper

    ssl_required :new, :create, :destroy, :update
    ssl_allowed :login_bar

    before_action :set_checkout_redirect, only: :create
    after_action :ensure_valid_locale_persisted, only: :create

    def create
      authenticate_spree_user!

      if spree_user_signed_in?
        respond_to do |format|
          format.html {
            flash[:success] = t('devise.success.logged_in_succesfully')
            redirect_back_or_default(after_sign_in_path_for(spree_current_user))
          }
          format.js {
            render json: { email: spree_current_user.login }, status: :ok
          }
        end
      else
        respond_to do |format|
          format.html {
            flash.now[:error] = t('devise.failure.invalid')
            render :new
          }
          format.js {
            render json: { message: t('devise.failure.invalid') }, status: :unauthorized
          }
        end
      end
    end

    private

    def accurate_title
      Spree.t(:login)
    end

    def redirect_back_or_default(default)
      redirect_to(session["spree_user_return_to"] || default)
      session["spree_user_return_to"] = nil
    end

    def ensure_valid_locale_persisted
      # When creating a new user session we have to wait until after a successful
      # login to be able to persist a locale on the current user
      return unless spree_current_user && !available_locale?(spree_current_user.locale)

      spree_current_user.update!(locale: I18n.default_locale)
    end
  end
end

{'fraction_non_alphanumeric': 0.08377253814147018, 'fraction_numerical': 0.002219140083217753, 'mean_word_length': 2.740663900414938, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6338771', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1054, 'n_words': 221}