<commit_before>'use strict';

module.exports = function(app) {

	// var User = require('.././models/user')

	/*
	Res Data:
		code 	= int
		message = string
		token 	= string
		ttl		= number
	*/
	function createAccount(req, res) {
		var username	= req.body.username;
		var password	= req.body.password;
		var email		= req.body.email;

		// Captcha key

		// Check username exists
		// Check is valid username
		// Check username length

		// Check password length

		// Check is valid email
		// Check email length

		/*
		User.create(username, password, email, function(err, user) {
			if (err) {
				
				// Do stuff
				return;
			}

			// Set cookies
			// Logon???
			// Redirect
		});
		*/
	}

	/*
	Res Data:
		code 	= int
		message = string
	*/
	function deleteAccount(req, res) {
		// What is a token?????
		var token = req.body.token;

		/*
		User.delete(function(err) {
			if (err) {
				
				// Do stuff
				return;
			}

			// Success!
			// Redirect
		});
		*/
	}

	/*
	Res Data:
		code 	= int
		message = string
	*/
	function changePassword(req, res) {
		var token 			= req.body.token;
		var oldPassword 	= req.body.oldPassword;
		var newPassword 	= req.body.newPassword;
		var confirmPassword = req.body.confirmPassword;

		if (newPassword != confirmPassword) {

			// flash not matching
		} else {

			// Validate oldPassword

			/*
			User.changePassword(function(err) {
				if (err) {
					
					// Do stuff
					return;
				} 

				// Success!
				// Redirect
			};
			*/
		}
	}

	/*
	Res Data:
		code 	= int
		message = string
		token 	= string
		ttl		= number
	*/
	function authenticate(req, res) {
		var usenameEmail 	= req.body.usernameemail;
		var password 		= req.body.password;

		// Check if user already logged in

		// Check if usernameEmail field is empty
		// Check usernameEmail length
		// Check id exists (either username or email) ??

		// Check if password field is empty
		// Check password length
		// Validate password ??

		/*
		User.login(id, password, function(err, user) {
			if (err) {
				
				// Do stuff
				return;
			}
			User.setOnlineStatus(user.username, true, function(err) {
				if (err) {
					
					// Do stuff
					return;
				}

				// Acquire session cookie
				// Redirect
			})
		});
		*/
	}

	/*
	Res Data:
		code 	= int
		message = string
		token 	= string
		ttl		= number
	*/
	function reauthenticate(req, res) {
		var token = req.body.token;

		// ?????
	}

	/*
	Res Data:
		code 	= int
		message = string
	*/
	function logout(req, res) {

		// Check if user logged in already

		/*
		User.setOnlineStatus(user.username, false, function(err) {
				if (err) {

					// Do stuff
					return;
				}

				// Destroy session
				// Redirect
		*/
	}

	app.post('/v1/user', createAccount);
	app.delete('/v1/user', deleteAccount);

	app.put('/v1/user/password', changePassword);

	app.post('/v1/user/authenticate', authenticate);

	app.post('/v1/user/reauthenticate', reauthenticate);

	app.post('/v1/user/logout', logout);

	
}<commit_msg>Updated user.js<commit_after>'use strict';

module.exports = function(app) {

	var validator = require('validator');
	var User = require('/.././models/user');

	// Returns true if contains only alphanumeric characters,
	// underscores and/or dashes.
	var isUsername = function isUsername(username) {
		var re = /^[a-zA-Z0-9-_]+$/;
		return re.test(username);
	}

	/*
	Res Data:
		code 	= int
		message = string
		token 	= string
		ttl		= number
	*/
	var createAccount = function createAccount(req, res) {
		var username	= validator.toString(validator.escape(req.body.username));
		var password	= validator.toString(validator.escape(req.body.password));
		var email		= validator.toString(validator.escape(req.body.email));

		// Captcha key

		/////////////////////
		// USERNAME CHECKS //
		/////////////////////

		if (!username) {
			res.send({
				code 	: 400,
				message : 'Username field is required.'
			});
			return;
		}

		// Check username already exists
		/*
		if (req.session.user) {
			error = {
				code 	: 400,
				message : '..........'
			};
			res.send(error);
			return;
		}
		*/

		if (!isUsername(username)) {
			res.send({
				code 	: 400,
				message : 'Username can only contain alphanumeric characters, ' +
						  'underscores, and/or dashes.'
			});
			return;
		}

		// TODO: define min and max
		// Check username length
		if (!validator.isLength(username, min, max)) {
			res.send({
				code	: 400,
				message : 'Username must be between min and max characters.'
			});
			return;
		}

		/////////////////////
		// PASSWORD CHECKS //
		/////////////////////

		// TODO: Confirm Password?

		if (!password) {
			res.send({
				code 	: 400,
				message : 'Password field is required.'
			});
			return;
		}

		// Check is valid password (must contain at least one letter and number)
		
		// TODO: define min and max
		// Check password length
		if (!validator.isLength(password, min, max)) {
			res.send({
				code	: 400,
				message : 'Password must be between min and max characters.'
			});
			return;
		}

		//////////////////
		// EMAIL CHECKS //
		//////////////////

		// TODO: Is this check neccessary?
		// Check email field
		if (!email) {
			res.send({
				code 	: 400,
				message : 'Email field is required.'
			});
			return;
		}

		// Check email already exists

		if (!validator.isEmail(email)) {
			res.send({
				code	: 400,
				message : 'Not a valid email address.'
			});
			return;
		}

		// TODO: define min and max
		// Check email length
		if (!validator.isLength(email, min, max)) {
			res.send({
				code	: 400,
				message : 'Email must be between min and max characters long.'
			});
			return;
		}

		User.create(username, password, email, function(err, user) {
			if (err) {
				res.send(err);
				return;
			}
			User.setOnlineStatus(user.username, true, function(err) {
				if (err) {
					res.send(err);
					return;
				}
				req.session.user = user;

				// TODO: Decide session expiration time and change var time
				// Cookie expiration time
				var time;
				
				req.session.cookie.expires = new Date(Date.now() + time);
				
				res.send({
					code	: 200,
					message	: 'Account successfully created.'
					// token?
					// ttl?
				});

				// Redirect?
			});
		});
	}

	/*
	Res Data:
		code 	= int
		message = string
	*/
	var deleteAccount = function deleteAccount(req, res) {
		// var token = req.body.token;

		User.delete(function(err) {
			if (err) {
				res.send(err);
				return;
			}
			res.send({
				code	: 200,
				message	: 'Account successfully deleted.'
			});
		});
	}

	/*
	Res Data:
		code 	= int
		message = string
	*/
	var changePassword = function changePassword(req, res) {
		// var token 			= req.body.token;
		var oldPassword 	= validator.toString(validator.escape(req.body.oldPassword));
		var newPassword 	= validator.toString(validator.escape(req.body.newPassword));
		var confirmPassword = validator.toString(validator.escape(req.body.confirmPassword));
		var user 			= req.session.user;

		if (newPassword != confirmPassword) {
			res.send({
				code	: 400,
				message	: 'Passwords must match.'
			});
			return;
		}
		
		// Cryptography??
		if (oldPassword != user.password) {
			res.send({
				code	: 400,
				message : 'Incorrect password.'
			});
			return;
		}

		User.changePassword(function(err) {
			if (err) {
				res.send(err);
				return;
			} 
			res.send({
				code	: 200,
				message	: 'Password has been changed.'
			});
		};
	}

	/*
	Res Data:
		code 	= int
		message = string
		token 	= string
		ttl		= number
	*/
	var authenticate = function authenticate(req, res) {
		var usenameEmail 	= validator.toString(validator.escape(req.body.usernameemail));
		var password 		= validator.toString(validator.escape(req.body.password));

		// TODO: Isn't a redirect more appropriate?
		// Check if user already logged in
		if (req.session.user) {
			res.send({
				code	: 400
				message	: 'User already online.'
			});
			return;
		}

		if (!usernameEmail) {
			res.send({
				code	: 400
				message	: 'Username/Email field is required.'
			});
		}

		// Check usernameEmail length
		// Check id exists (either username or email) ??

		if (!password) {
			res.send({
				code 	: 400,
				message : 'Password field is required.'
			});
			return;
		}

		// Check password length
		// Validate password ??

		User.login(usernameEmail, password, function(err, user) {
			if (err) {
				res.send(err);
				return;
			}
			User.setOnlineStatus(user.username, true, function(err) {
				if (err) {
					res.send(err);
					return;
				}
				req.session.user = user;

				// TODO: Decide session expiration time and change var time
				// Cookie expiration time
				var time;
				
				req.session.cookie.expires = new Date(Date.now() + time);
				
				res.send({
					code	: 200,
					message	: 'Login successful.'
					// token?
					// ttl?
				});

				// Redirect??
			})
		});
	}

	/*
	Res Data:
		code 	= int
		message = string
		token 	= string
		ttl		= number
	*/
	var reauthenticate = function reauthenticate(req, res) {
		var token = req.body.token;

		// ?????
	}

	/*
	Res Data:
		code 	= int
		message = string
	*/
	var logout = function logout(req, res) {

		if (!req.session.user) {
			res.send({
				code	: 400,
				message	: 'User not logged in.'
			});
			return;
		}

		User.setOnlineStatus(user.username, false, function(err) {
			if (err) {
				res.send(err);
				return;
			}
			req.session.destroy(function(err) {
				if (err) {
					res.send(err);
					return;
				}
				res.send({
					code	: 200,
					message	: 'Logout successful.'
				});

				// Redirect
			});		
		});
	}

	app.post('/v1/user', createAccount);
	app.delete('/v1/user', deleteAccount);

	// '/v1/user/:userid/password'?
	app.put('/v1/user/password', changePassword);

	app.post('/v1/user/authenticate', authenticate);

	app.post('/v1/user/reauthenticate', reauthenticate);

	app.post('/v1/user/logout', logout);

	
}
{'fraction_non_alphanumeric': 0.1527133346782328, 'fraction_numerical': 0.0075650595118014925, 'mean_word_length': 2.2025193798449614, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2431610', 'n_tokens_mistral': 4249, 'n_tokens_neox': 3340, 'n_words': 820}