<commit_before>module Api
  class SessionsController < ApplicationController
    skip_before_action :authenticate_user_from_token!

    # POST /user/login
    def create
      @user = User.find_for_database_authentication(email: params[:username])
      return invalid_login_attempt unless @user

      if @user.valid_password?(params[:password])
        sign_in :user, @user
        render json: @user, serializer: SessionSerializer, root: nil
      else
        invalid_login_attempt
      end
    end

    private

    def invalid_login_attempt
      warden.custom_failure!
      render json: {error: t('sessions_controller.invalid_login_attempt')}, status: :unprocessable_entity
    end

  end
end<commit_msg>Add logout function and change error message for invalid login attempt<commit_after>module Api
  class SessionsController < ApplicationController
    skip_before_action :authenticate_user_from_token!

    # POST /user/login
    def create
      @user = User.find_for_database_authentication(email: params[:username])
      return invalid_login_attempt unless @user

      if @user.valid_password?(params[:password])
        sign_in :user, @user
        render json: @user, serializer: SessionSerializer, root: nil
      else
        invalid_login_attempt
      end
    end

    def destroy
      warden.logout
      render json: {} 
    end

    private

    def invalid_login_attempt
      warden.custom_failure!
      render json: {error: ('Incorrect username/password')}, status: :unprocessable_entity
    end

  end
end
{'fraction_non_alphanumeric': 0.06770833333333333, 'fraction_numerical': 0.0, 'mean_word_length': 3.293296089385475, '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': '2135947', 'n_tokens_mistral': 441, 'n_tokens_neox': 419, 'n_words': 110}