<commit_before>const config = require('../config.js')
const db = require('knex')(config.database)
const bcrypt = require('bcrypt')
const saltRounds = 10
const randomstring = require('randomstring')

let authController = {}

authController.verify = function(req, res, next){
	let username = req.body.username
	let password = req.body.password

	if(username === undefined) return res.json({ success: false, description: 'No username provided' })
	if(password === undefined) return res.json({ success: false, description: 'No password provided' })

	db.table('users').where('username', username).then((user) => {
		if(user.length === 0) return res.json({ success: false, description: 'Username doesn\'t exist' })

		bcrypt.compare(password, user[0].password, function(err, result) {
			if(result === false) return res.json({ success: false, description: 'Wrong password' })
			return res.json({ success: true, token: user[0].token })
		})
	}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })

}

authController.register = function(req, res, next){

	if(config.enableUserAccounts === false) 
		return res.json({ success: false, description: 'Register is disabled at the moment' })

	let username = req.body.user
	let password = req.body.password

	if(username === undefined) return res.json({ success: false, description: 'No username provided' })
	if(password === undefined) return res.json({ success: false, description: 'No password provided' })

	if(username.length < 6 || username.length > 32)
		return res.json({ success: false, description: 'Username must have 6-32 characters' })
	if(password.length < 6 || password.length > 64)
		return res.json({ success: false, description: 'Password must have 6-64 characters' })

	db.table('users').where('username', username).then((user) => {
		if(user.length !== 0) return res.json({ success: false, description: 'Username already exists' })

		bcrypt.hash(password, saltRounds, function(err, hash) {
			if(err) return res.json({ success: false, description: 'Error generating password hash (╯°□°）╯︵ ┻━┻' })

			let token = randomstring.generate(64)

			db.table('users').insert({
				username: username,
				password: <PASSWORD>,
				token: token
			}).then(() => {
				return res.json({ success: true, token: token})
			}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
		})

	}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })

}

authController.changePassword = function(req, res, next){

	let token = req.headers.token
	if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })

	db.table('users').where('token', token).then((user) => {
		if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
		
		let password = req.body.password
		if(password === undefined) return res.json({ success: false, description: 'No password provided' })
		if(password.length < 6 || password.length > 64)
			return res.json({ success: false, description: 'Password must have 6-64 characters' })

		bcrypt.hash(password, saltRounds, function(err, hash) {
			if(err) return res.json({ success: false, description: 'Error generating password hash (╯°□°）╯︵ ┻━┻' })

			db.table('users').where('id', user.id).update({password: hash}).then(() => {
				return res.json({ success: true})
			}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
		})
	}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })

}

module.exports = authController<commit_msg>derp<commit_after>const config = require('../config.js')
const db = require('knex')(config.database)
const bcrypt = require('bcrypt')
const saltRounds = 10
const randomstring = require('randomstring')

let authController = {}

authController.verify = function(req, res, next){
	
	let username = req.body.username
	let password = req.body.password

	if(username === undefined) return res.json({ success: false, description: 'No username provided' })
	if(password === undefined) return res.json({ success: false, description: 'No password provided' })

	db.table('users').where('username', username).then((user) => {
		if(user.length === 0) return res.json({ success: false, description: 'Username doesn\'t exist' })

		bcrypt.compare(password, user[0].password, function(err, result) {
			if(result === false) return res.json({ success: false, description: 'Wrong password' })
			return res.json({ success: true, token: user[0].token })
		})
	}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })

}

authController.register = function(req, res, next){

	if(config.enableUserAccounts === false) 
		return res.json({ success: false, description: 'Register is disabled at the moment' })

	let username = req.body.user
	let password = req.body.password

	if(username === undefined) return res.json({ success: false, description: 'No username provided' })
	if(password === undefined) return res.json({ success: false, description: 'No password provided' })

	if(username.length < 6 || username.length > 32)
		return res.json({ success: false, description: 'Username must have 6-32 characters' })
	if(password.length < 6 || password.length > 64)
		return res.json({ success: false, description: 'Password must have 6-64 characters' })

	db.table('users').where('username', username).then((user) => {
		if(user.length !== 0) return res.json({ success: false, description: 'Username already exists' })

		bcrypt.hash(password, saltRounds, function(err, hash) {
			if(err) return res.json({ success: false, description: 'Error generating password hash (╯°□°）╯︵ ┻━┻' })

			let token = randomstring.generate(64)

			db.table('users').insert({
				username: username,
				password: <PASSWORD>,
				token: token
			}).then(() => {
				return res.json({ success: true, token: token})
			}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
		})

	}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })

}

authController.changePassword = function(req, res, next){

	let token = req.headers.token
	if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })

	db.table('users').where('token', token).then((user) => {
		if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
		
		let password = req.body.password
		if(password === undefined) return res.json({ success: false, description: 'No password provided' })
		if(password.length < 6 || password.length > 64)
			return res.json({ success: false, description: 'Password must have 6-64 characters' })

		bcrypt.hash(password, saltRounds, function(err, hash) {
			if(err) return res.json({ success: false, description: 'Error generating password hash (╯°□°）╯︵ ┻━┻' })

			db.table('users').where('id', user.id).update({password: <PASSWORD>}).then(() => {
				return res.json({ success: true})
			}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
		})
	}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })

}

module.exports = authController
{'fraction_non_alphanumeric': 0.17277274570771933, 'fraction_numerical': 0.00892253616330945, 'mean_word_length': 5.049059689288635, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 22, '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': '4306162', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2198, 'n_words': 607}