<commit_before>class AuthenticationsController < ApplicationController

	def login
		if signed_in?
			redirect_to root_path, :flash => { :success => "Already logged in!" }
		else
			redirect_to auth_url
		end
	end

	def callback

		# Catch any errors gracefully
		begin

			# Check for the state (to avoid forgery)
			if params[:state] != state
				raise "state error"
			else
				delete_state!
			end

			# Set params for GitHub POST request
			req_params = Hash.new
			req_params[:client_id] = ENV['GITHUB_CLIENT_ID']
			req_params[:client_secret] = ENV['GITHUB_CLIENT_SECRET']
			req_params[:code] = params[:code]

			# Make POST request
			response = post_request('https://github.com', '/login/oauth/access_token', req_params)

			# Create client with response
			access_token = response['access_token']
			Octokit.connection_options[:ssl] = {:ca_file => File.join(Rails.root, 'config', 'cacert.pem')}
			client = Octokit::Client.new :access_token => access_token

			# Save the local User to the database
			user = User.find_by_access_token(access_token)

			if user.present?
				user.update(:username => client.user[:login], :name => client.user[:name], :gravatar_id => client.user[:gravatar_id])
			else
				user = User.create(:username => client.user[:login], :name => client.user[:name], :gravatar_id => client.user[:gravatar_id], :access_token => access_token)
			end

			# Log in the user
			session[:user_id] = user.id

			# Check the rate limit
			if client.rate_limit.remaining < 10
				redirect_to login_rate_limited_path
			end

			# Delete any outdated repos
			user.repositories.each do |repo|
				if client.repos.select {|r| r[:name] == repo.name }.count == 0
					repo.destroy
				end
			end

			# Add their repositories
			client.repos.each do |api_repo|

				# See if the repo exists
				repo = Repository.find_by_github_id(api_repo.id)

				# If so, update its name
				if repo
					repo.update(:name => api_repo[:name])
				# Else create it
				else

					# Create the new repo
					repo = Repository.new(:github_id => api_repo[:id], :name => api_repo[:name], :is_active => false)

					# Tie the repo to the user
					repo.user = user

					# Save the new repo
					repo.save

				end

			end

			# Add their org repositories (if any), under the org name to keep it nice and neat
			client.orgs.each do |api_org|

				# See if the org exists
				org = Organization.find_by_name(api_org[:login])

				# If not, create it
				if org.nil?

					# Create the new org
					org = Organization.new(:name => api_org[:login])
<commit_msg>Fixed bug where organizations would not go away in app if deleted<commit_after>class AuthenticationsController < ApplicationController

	def login
		if signed_in?
			redirect_to root_path, :flash => { :success => "Already logged in!" }
		else
			redirect_to auth_url
		end
	end

	def callback

		# Catch any errors gracefully
		begin

			# Check for the state (to avoid forgery)
			if params[:state] != state
				raise "state error"
			else
				delete_state!
			end

			# Set params for GitHub POST request
			req_params = Hash.new
			req_params[:client_id] = ENV['GITHUB_CLIENT_ID']
			req_params[:client_secret] = ENV['GITHUB_CLIENT_SECRET']
			req_params[:code] = params[:code]

			# Make POST request
			response = post_request('https://github.com', '/login/oauth/access_token', req_params)

			# Create client with response
			access_token = response['access_token']
			Octokit.connection_options[:ssl] = {:ca_file => File.join(Rails.root, 'config', 'cacert.pem')}
			client = Octokit::Client.new :access_token => access_token

			# Save the local User to the database
			user = User.find_by_access_token(access_token)

			if user.present?
				user.update(:username => client.user[:login], :name => client.user[:name], :gravatar_id => client.user[:gravatar_id])
			else
				user = User.create(:username => client.user[:login], :name => client.user[:name], :gravatar_id => client.user[:gravatar_id], :access_token => access_token)
			end

			# Log in the user
			session[:user_id] = user.id

			# Check the rate limit
			if client.rate_limit.remaining < 10
				redirect_to login_rate_limited_path
			end

			# Delete any outdated repos
			user.repositories.each do |repo|
				if client.repos.select {|r| r[:name] == repo.name }.count == 0
					repo.destroy
				end
			end

			# Add their repositories
			client.repos.each do |api_repo|

				# See if the repo exists
				repo = Repository.find_by_github_id(api_repo.id)

				# If so, update its name
				if repo
					repo.update(:name => api_repo[:name])
				# Else create it
				else

					# Create the new repo
					repo = Repository.new(:github_id => api_repo[:id], :name => api_repo[:name], :is_active => false)

					# Tie the repo to the user
					repo.user = user

					# Save the new repo
					repo.save

				end

			end

			# Delete any outdated orgs
			user.organizations.each do |org|
				if client.orgs.select {|r| r[:login] == org.name }.count == 0
					org.destroy
				end
			end

			# Add their org repositories (if any), under the org name to keep it nice and neat
			client.orgs.each do |api_org|

				# See if the org exists
				org = Organization.find_by_name(api_org[:login])

				# If not, create it
				if org.nil?

					# Create the new org
					org = Organization.new(:name => api_org[:login])

{'fraction_non_alphanumeric': 0.10985915492957747, 'fraction_numerical': 0.0013145539906103286, 'mean_word_length': 3.233704292527822, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 2, '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': '5315997', 'n_tokens_mistral': 2128, 'n_tokens_neox': 1690, 'n_words': 488}