<commit_before>class Api::V1::SessionsController < Api::V1Controller

  # Public: Creates a redis backed session for the requesting client if
  # CSRF token is available. Otherwise the application stays stateless.
  # If the oAuth hash is supplied it will override the email & password
  # parameters and try authenticating with those insted. If authentication
  # fails, it will fall back on email & password if supplied.
  #
  # email - The registered email for a user
  # password - <PASSWORD> that <PASSWORD>
  #
  # oauth - A Hash:
  #         :provider - String with the service oAuth provider
  #                     Can either be "facebook" or "twitter"
  #
  #         :uid -      String with the user id with the provider
  #
  #         :token -    String with the secret token for the user to allow the
  #                     application to access and authenticate.
  #
  #         :cli_type - String with the type of the client.
  #                     Currently supports "ios" or "android"
  #
  # Returns the session template defined in 'views/api/v1/sessions/base.rabl'.
  def create
    
    # EXCEPTION HANDLING - TODO: MAKE PRETTY
    if params[:oauth] && params[:oauth][:token] != BCrypt::Engine.hash_secret("#{params[:oauth][:uid]}#{params[:oauth][:provider]}",INTERNAL_TOKEN)
    
      render_exception("You don't have access to this service.", 403)
    
    elsif (params[:password].nil? && params[:email].nil?) && ( params[:oauth].try(:fetch, :provider).nil? && params[:oauth].try(:fetch, :uid).nil? )
      
      set_flash_message message: "You did not supply valid credentials.", title: "Login error!", type: "error"
      render_exception "You need to specify a valid authentication method. With either oAuth or Email/Password", 400
      
    end
    
    @current_user = User.authenticate(
      email: params[:email],
      password: params[:password],
      oauth: params[:oauth]
    )
    
    # MORE EXCEPTION HANDLING - TODO: MAKE EVEN PRETTIER
    if !@current_user.has_a_valid_authentication_method? && params[:oauth]
      set_flash_message(
          message: "Could not connect #{params[:oauth][:provider]} to any account, please login to connect your #{params[:oauth][:provider]}. If you have no account, one will be created for you.",
          type: "error",
          title: "Almost there!"
      )
    elsif !@current_user.has_a_valid_authentication_method?
      set_flash_message(
          message: "Could not authenticate your account please look over your credentials. Maybe you don't have an account, why don't you create one?",<commit_msg>Fixed nil bug in Api::V1::SessionsController#create<commit_after>class Api::V1::SessionsController < Api::V1Controller

  # Public: Creates a redis backed session for the requesting client if
  # CSRF token is available. Otherwise the application stays stateless.
  # If the oAuth hash is supplied it will override the email & password
  # parameters and try authenticating with those insted. If authentication
  # fails, it will fall back on email & password if supplied.
  #
  # email - The registered email for a user
  # password - <PASSWORD>
  #
  # oauth - A Hash:
  #         :provider - String with the service oAuth provider
  #                     Can either be "facebook" or "twitter"
  #
  #         :uid -      String with the user id with the provider
  #
  #         :token -    String with the secret token for the user to allow the
  #                     application to access and authenticate.
  #
  #         :cli_type - String with the type of the client.
  #                     Currently supports "ios" or "android"
  #
  # Returns the session template defined in 'views/api/v1/sessions/base.rabl'.
  def create
    
    # EXCEPTION HANDLING - TODO: MAKE PRETTY
    if params[:oauth].present? && params[:oauth].try(:fetch, :token) != BCrypt::Engine.hash_secret("#{params[:oauth][:uid]}#{params[:oauth][:provider]}",INTERNAL_TOKEN)
    
      render_exception("You don't have access to this service.", 403)
    
    elsif (params[:password].nil? && params[:email].nil?) && ( params[:oauth].try(:fetch, :provider).nil? && params[:oauth].try(:fetch, :uid).nil? )
      
      set_flash_message message: "You did not supply valid credentials.", title: "Login error!", type: "error"
      render_exception "You need to specify a valid authentication method. With either oAuth or Email/Password", 400
      
    end
    
    @current_user = User.authenticate(
      email: params[:email],
      password: params[:password],
      oauth: params[:oauth]
    )
    
    # MORE EXCEPTION HANDLING - TODO: MAKE EVEN PRETTIER
    if !@current_user.has_a_valid_authentication_method? && params[:oauth]
      set_flash_message(
          message: "Could not connect #{params[:oauth][:provider]} to any account, please login to connect your #{params[:oauth][:provider]}. If you have no account, one will be created for you.",
          type: "error",
          title: "Almost there!"
      )
    elsif !@current_user.has_a_valid_authentication_method?
      set_flash_message(
          message: "Could not authenticate your account please look over your credentials. Maybe you don't have an account, why don't you create one?",
{'fraction_non_alphanumeric': 0.10250525913176516, 'fraction_numerical': 0.003633581946834959, 'mean_word_length': 2.968133535660091, 'pattern_counts': {'":': 0, '<': 8, '<?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': '2002774', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1453, 'n_words': 514}