<commit_before>class ApplicationController < ActionController::API
  #protect_from_forgery with: :null_session

  before_action :authenticate_user_from_token!, except: [:preflight]

  respond_to :json

  def preflight
    render nothing: true
  end

  def authenticate_user_from_token!
    auth_token = request.headers['Authorization']

    if auth_token
      authenticate_with_auth_token auth_token
    else
      authentication_error
    end
  end

  private

  def authenticate_with_auth_token auth_token
    unless auth_token.include?(':')
      authentication_error
      return
    end

    user_id = auth_token.split(':').first
    user = User.where(id: user_id).first

    if user && Devise.secure_compare(user.access_token, auth_token)
      sign_in user, store: false
    else
      authentication_error
    end
  end

  def authentication_error
    render json: {error: 'unauthorized'}, status: 401
  end
end<commit_msg>Fix access_token authentication now we're using secure random<commit_after>class ApplicationController < ActionController::API
  #protect_from_forgery with: :null_session

  before_action :authenticate_user_from_token!, except: [:preflight]

  respond_to :json

  def preflight
    render nothing: true
  end

  def authenticate_user_from_token!
    auth_token = request.headers['Authorization']

    if auth_token
      authenticate_with_auth_token auth_token
    else
      authentication_error
    end
  end

  private

  def authenticate_with_auth_token auth_token
    user = User.find_by(access_token: auth_token)

    if user && Devise.secure_compare(user.access_token, auth_token)
      sign_in user, store: false
    else
      authentication_error
    end
  end

  def authentication_error
    render json: {error: 'unauthorized'}, status: 401
  end
end
{'fraction_non_alphanumeric': 0.061872909698996656, 'fraction_numerical': 0.0033444816053511705, 'mean_word_length': 3.838274932614555, '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': '1201803', 'n_tokens_mistral': 562, 'n_tokens_neox': 528, 'n_words': 131}