<commit_before>class AuthenticationsController < ApplicationController
  def skip_store_location
    true
  end

  def create
    auth = request.env["omniauth.auth"]
    if authenticate!(auth['provider'], auth['uid'], auth['user_info'])
      destination_url = stored_location_for(:user) || root_path
      redirect_to destination_url, :notice => "Welcome #{current_user.name}!"
    end
  end

  def failure
    redirect_to root_url, :notice => "Authentication error: #{params[:message].humanize}"
  end

  private
  def sign_in_with_authentication(authentication)
    if user_signed_in?
      current_user.authentications.create(provider: authentication.provider, uid: uid)
    else
      sign_in authentication.user
    end
    true
  end

  def sign_in_with_twitter(uid)
    if user_signed_in?
      current_user.authentications.create(provider: 'twitter', uid: uid)
      true
    else
      redirect_to new_user_session_path, notice: I18n.t('auth.cant_create_twitter')
      false
    end
  end

  def create_authentication_and_sign_in(provider, uid, auth_info)
    user = User.create_from_auth_info(provider, uid, auth_info)
    sign_in(user)
    true
  end

  def create_authentication(provider, uid, auth_info)
    if provider.to_s == 'twitter'
      sign_in_with_twitter(uid)
    else
      create_authentication_and_sign_in(provider, uid, auth_info)
    end
  end

  def authenticate!(provider, uid, user_info)
    auth_hash = request.env['omniauth.auth']
    authentication = Authentication.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
    if authentication
      sign_in_with_authentication(authentication)
    else
      create_authentication(provider, uid, auth_hash['info'])
    end
  end
end<commit_msg>Refactor: :scissor: Reduce the conditionals and bumps to aggregate authentications for signed_in users. <commit_after>class AuthenticationsController < ApplicationController
  def skip_store_location
    true
  end

  def create
    auth = request.env["omniauth.auth"]
    destination_url = stored_location_for(:user) || root_path

    if user_signed_in?
      current_user.authentications.create(provider: auth['provider'], uid: auth['uid'])
      will_redirect = true
    else
      # As soon as we update the rails version, this will redirect will be
      # sacrificed to the gods of the good code.
      # It is here now because the redirect_to in our version doesn't do the
      # shortcircuit. And the authenticate! method, can call redirections.
      will_redirect = authenticate!(auth['provider'], auth['uid'], auth['user_info'])
    end

    redirect_to destination_url, notice: "Welcome #{current_user.name}!" if will_redirect
  end

  def failure
    redirect_to root_url, notice: "Authentication error: #{params[:message].humanize}"
  end

  private

  def create_authentication_and_sign_in(provider, uid, auth_info)
    if provider.to_s == 'twitter'
      redirect_to new_user_session_path, notice: I18n.t('auth.cant_create_twitter')
      false
    else
      user = User.create_from_auth_info(provider, uid, auth_info)
      sign_in(user)
      true
    end
  end

  def authenticate!(provider, uid, user_info)
    auth_hash = request.env['omniauth.auth']
    authentication = Authentication.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
    if authentication
      sign_in authentication.user
      true
    else
      create_authentication_and_sign_in(provider, uid, auth_hash['info'])
    end
  end
end
{'fraction_non_alphanumeric': 0.08613264427217916, 'fraction_numerical': 0.0011484352569623888, 'mean_word_length': 3.984263233190272, 'pattern_counts': {'":': 0, '<': 5, '<?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': '124398', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1033, 'n_words': 252}