<commit_before>require 'jwt'

class User < ActiveRecord::Base
  devise :omniauthable, :omniauth_providers => [:orcid, :github, :persona]

  has_many :applications, class_name: 'Doorkeeper::Application', as: :owner

  scope :query, ->(query) { where("name like ? OR email like ? OR uid = ?", "%#{query}%", "%#{query}%", "%#{query}%") }

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create(generate_user(auth))
  end

  # Helper method to check for admin user
  def is_admin?
    role == "admin"
  end

  def api_key
    authentication_token
  end

  def orcid
    if provider == 'orcid'
      "http://orcid.org/#{uid}"
    end
  end

  def self.generate_user(auth)
    authentication_token = generate_authentication_token

    if User.count > 0 || Rails.env.test?
      role = "user"
    else
      role = "admin"
    end

    { email: auth.info.email,
      name: auth.info.name,
      authentication_token: authentication_token,
      role: role }
  end

  def jwt_payload
    claims = {
      uid: uid,
      name: name,
      email: email,
      role: role,
      api_key: api_key,
      iat: Time.now.to_i
    }

    JWT.encode(claims, ENV['JWT_SECRET_KEY'])
  end

  private

  def self.generate_authentication_token
    loop do
      token = Devise.friendly_token
      break token unless User.where(authentication_token: token).first
    end
  end
end<commit_msg>don't require email for registration<commit_after>require 'jwt'

class User < ActiveRecord::Base
  devise :omniauthable, :omniauth_providers => [:orcid, :github, :persona]

  has_many :applications, class_name: 'Doorkeeper::Application', as: :owner

  scope :query, ->(query) { where("name like ? OR email like ? OR uid = ?", "%#{query}%", "%#{query}%", "%#{query}%") }

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create(generate_user(auth))
  end

  # Helper method to check for admin user
  def is_admin?
    role == "admin"
  end

  def api_key
    authentication_token
  end

  def orcid
    if provider == 'orcid'
      "http://orcid.org/#{uid}"
    end
  end

  def self.generate_user(auth)
    authentication_token = generate_authentication_token

    if User.count > 0 || Rails.env.test?
      role = "user"
    else
      role = "admin"
    end

    { name: auth.info.name,
      authentication_token: authentication_token,
      role: role }
  end

  def jwt_payload
    claims = {
      uid: uid,
      name: name,
      email: email,
      role: role,
      api_key: api_key,
      iat: Time.now.to_i
    }

    JWT.encode(claims, ENV['JWT_SECRET_KEY'])
  end

  private

  def self.generate_authentication_token
    loop do
      token = Devise.friendly_token
      break token unless User.where(authentication_token: token).first
    end
  end
end
{'fraction_non_alphanumeric': 0.11725115125752746, 'fraction_numerical': 0.0007084661707403471, 'mean_word_length': 2.9718706047819974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1310034', 'n_tokens_mistral': 979, 'n_tokens_neox': 945, 'n_words': 235}