<commit_before># frozen_string_literal: true

require_dependency "#{Rails.root}/lib/importers/user_importer"

#= Controller for OmniAuth authentication callbacks
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  include Devise::Controllers::Rememberable

  def mediawiki
    set_user_from_auth_hash { return }

    unless @user.persisted?
      redirect_to root_url
      return
    end

    remember_me @user
    register_first_login

    # Here, we save the user from having to click 'Join' to confirm their enrollment.
    # If they started from an enroll link, redirect them directly to the enroll action.
    if returning_from_enroll_link?
      sign_in @user
      redirect_to "/courses/#{session['course_slug']}/enroll/#{session['enroll_code']}"
    else
      sign_in_and_redirect @user
    end
  end

  protected

  def set_user_from_auth_hash
    auth_hash = request.env['omniauth.auth']
    if login_failed?(auth_hash)
      handle_login_failure(auth_hash)
      return yield
    end

    @user = UserImporter.from_omniauth(auth_hash)
  end

  def handle_login_failure(auth_hash)
    Rails.logger.info "OAuth login failed: #{auth_hash}"
    Raven.capture_message 'OAuth login failed',
                          level: 'warning',
                          extra: auth_hash[:extra][:raw_info]
    return redirect_to errors_login_error_path
  end

  def login_failed?(auth_hash)
    auth_hash[:extra]&.dig('raw_info', 'login_failed')
  end

  def register_first_login
    return if @user.first_login.present?
    @user.update(first_login: Time.now.utc)
  end

  def returning_from_enroll_link?
    return false unless session_course_slug
    origin = request.env['omniauth.origin']
    # the origin URL will include the course slug and the enroll code
    # if the user logged in from the 'enroll' view.
    origin[session_course_slug] && origin[session_enroll_code]
  end

  def session_course_slug
    session['course_slug']
  end

  def session_enroll_code
    session['enroll_code']
  end
end<commit_msg>Handle rare edge case error in login flow<commit_after># frozen_string_literal: true

require_dependency "#{Rails.root}/lib/importers/user_importer"

#= Controller for OmniAuth authentication callbacks
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  include Devise::Controllers::Rememberable

  def mediawiki
    set_user_from_auth_hash { return }

    unless @user.persisted?
      redirect_to root_url
      return
    end

    remember_me @user
    register_first_login

    # Here, we save the user from having to click 'Join' to confirm their enrollment.
    # If they started from an enroll link, redirect them directly to the enroll action.
    if returning_from_enroll_link?
      sign_in @user
      redirect_to "/courses/#{session['course_slug']}/enroll/#{session['enroll_code']}"
    else
      sign_in_and_redirect @user
    end
  end

  protected

  def set_user_from_auth_hash
    auth_hash = request.env['omniauth.auth']
    if login_failed?(auth_hash)
      handle_login_failure(auth_hash)
      return yield
    end

    @user = UserImporter.from_omniauth(auth_hash)
  end

  def handle_login_failure(auth_hash)
    Rails.logger.info "OAuth login failed: #{auth_hash}"
    Raven.capture_message 'OAuth login failed',
                          level: 'warning',
                          extra: auth_hash[:extra][:raw_info]
    return redirect_to errors_login_error_path
  end

  def login_failed?(auth_hash)
    auth_hash[:extra]&.dig('raw_info', 'login_failed')
  end

  def register_first_login
    return if @user.first_login.present?
    @user.update(first_login: Time.now.utc)
  end

  def returning_from_enroll_link?
    return false unless session_course_slug
    origin = request.env['omniauth.origin']
    return false unless origin
    # the origin URL will include the course slug and the enroll code
    # if the user logged in from the 'enroll' view.
    origin[session_course_slug] && origin[session_enroll_code]
  end

  def session_course_slug
    session['course_slug']
  end

  def session_enroll_code
    session['enroll_code']
  end
end
{'fraction_non_alphanumeric': 0.0748973181928002, 'fraction_numerical': 0.0, 'mean_word_length': 3.579646017699115, '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': '5928945', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1265, 'n_words': 318}