<commit_before>class SessionsController < Devise::SessionsController

  def create
    user_class = nil

    # Copy user data to ldap_user and local_user
    request.params['ldap_user'] = request.params['registered_user'] = request.params['user']

    user = User.where(username: request.params['user']['username']).first

    if user.is_a? LdapUser
      # Try to authenticate as a directory user.
      user_class = :ldap_user
    elsif user.is_a? RegisteredUser
      # Try to authenticate as a database user.
      user_class = :registered_user
    elsif user.nil?
      user_class = :ldap_user
    end

    # Use Warden to authenticate the user, if we get nil back, it failed.
    self.resource = warden.authenticate scope: user_class

    if self.resource.nil?
      flash[:error] = t 'sessions.create.failure'
      return redirect_to new_session_path
    end


    # Now we know the user is authenticated, sign them in to the site with Devise
    # At this point, self.resource is a valid user account.
    if user_class == :registered_user
      sign_in(user_class, self.resource)
    end

    respond_with self.resource, :location => after_sign_in_path_for(self.resource)
  end

  def destroy
    sign_out
    #set_flash_message :notice, :signed_out if sign_out && is_navigational_format?
<commit_msg>better error handling on sessions#create<commit_after>class SessionsController < Devise::SessionsController

  def create
    user_class = nil

    # Copy user data to ldap_user and local_user
    request.params['ldap_user'] = request.params['registered_user'] = request.params['user']

    user = User.where(username: request.params['user']['username']).first

    if user.is_a? LdapUser
      # Try to authenticate as a directory user.
      user_class = :ldap_user
    elsif user.is_a? RegisteredUser
      # Try to authenticate as a database user.
      user_class = :registered_user
    elsif user.nil?
      user_class = :ldap_user
    end

    # Use Warden to authenticate the user, if we get nil back, it failed.
    begin
      self.resource = warden.authenticate scope: user_class
    rescue Exception => e
        Rails.logger.info("Session create error: #{e.message}")
       self.resource = nil
    end

    if self.resource.nil?
      flash[:error] = t 'sessions.create.failure'
      return redirect_to new_session_path
    end


    # Now we know the user is authenticated, sign them in to the site with Devise
    # At this point, self.resource is a valid user account.
    if user_class == :registered_user
      sign_in(user_class, self.resource)
    end

    respond_with self.resource, :location => after_sign_in_path_for(self.resource)
  end

  def destroy
    sign_out
    #set_flash_message :notice, :signed_out if sign_out && is_navigational_format?

{'fraction_non_alphanumeric': 0.07968413496051686, 'fraction_numerical': 0.0, 'mean_word_length': 3.2679938744257275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '6255641', 'n_tokens_mistral': 865, 'n_tokens_neox': 829, 'n_words': 253}