<commit_before>
Promise = require 'bluebird'

errors = require 'errors'

{threshold} = require 'limits'

exports.$endpoint = ->
	
	limiter:
		message: "You are logging in too much."
		threshold: threshold(3).every(30).seconds()

	receiver: (req, fn) ->
		
		{passport} = req
		
		loginPromise = switch req.body.method
			
			when 'local'
				
				deferred = Promise.defer()
				
				passport.authenticate('local', deferred.callback)(
					req, res = {}, fn
				)
				
				Promise.settle([
					
					deferred.promise.bind({}).spread((@user, info) ->
						throw errors.instantiate 'login' unless @user
						
						(Promise.promisify req.login, req) @user
						
					).then(->
						
						@user.redactFor @user
					)
					
# If no user is found, the rejection will happen a lot sooner than if one is;
# password hashing must be done in the latter case. We'll create an artificial
# floor of 1 second to make it harder for an attacker to tell if a valid
# username was used.

					new Promise (resolve, reject) -> setTimeout resolve, 1000

				]).then ([userPromiseInspector]) ->

					if userPromiseInspector.isFulfilled()
						userPromiseInspector.value()
					else
						throw userPromiseInspector.error()
		
# TODO: Using nodeify here crashes the app. It may be a bluebird bug.
		
#		loginPromise.nodeify fn	
		loginPromise.then(
			(result) -> fn null, result
			(error) -> fn error
		)

exports.$errorType = (require 'client/modules/packages/user/login').$errorType
<commit_msg>Style<commit_after>
Promise = require 'bluebird'

errors = require 'errors'

{threshold} = require 'limits'

exports.$endpoint = ->
	
	limiter:
		message: "You are logging in too much."
		threshold: threshold(3).every(30).seconds()

	receiver: (req, fn) ->
		
		{passport} = req
		
		loginPromise = switch req.body.method
			
			when 'local'
				
				deferred = Promise.defer()
				
				passport.authenticate('local', deferred.callback)(
					req, res = {}, fn
				)
				
				Promise.settle([
					
					deferred.promise.bind({}).spread((@user, info) ->
						throw errors.instantiate 'login' unless @user
						
						(Promise.promisify req.login, req) @user
						
					).then -> @user.redactFor @user
					
# If no user is found, the rejection will happen a lot sooner than if one is;
# password hashing must be done in the latter case. We'll create an artificial
# floor of 1 second to make it harder for an attacker to tell if a valid
# username was used.

					new Promise (resolve, reject) -> setTimeout resolve, 1000

				]).then ([userPromiseInspector]) ->

					if userPromiseInspector.isFulfilled()
						userPromiseInspector.value()
					else
						throw userPromiseInspector.error()
		
# TODO: Using nodeify here crashes the app. It may be a bluebird bug.
		
#		loginPromise.nodeify fn	
		loginPromise.then(
			(result) -> fn null, result
			(error) -> fn error
		)

exports.$errorType = (require 'client/modules/packages/user/login').$errorType

{'fraction_non_alphanumeric': 0.11133740665308893, 'fraction_numerical': 0.005431093007467753, 'mean_word_length': 2.8223086900129704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '3083538', 'n_tokens_mistral': 1151, 'n_tokens_neox': 859, 'n_words': 284}