<commit_before>'use strict';

/**
 * @file Manages login API endpoints
 */

var auth = require(__dirname + '/../libs/auth.js');
var jwt  = require(__dirname + '/../libs/jwt.js');

module.exports = function(app, db) {

	app.post('/auth/login', function(req, res) {
		if(req.user) {
			res.json({
				error  : 'You\'re already logged in, silly!',
				success: false,
				jwt    : null
			});
			return;
		}

		var rememberMe = typeof req.body.remember !== 'undefined';

		auth.login(db, req.body.user, req.body.password, rememberMe, function(err, response, jwt) {
			if(err) {
				var errorMessage = err.message;
			} else {
				var errorMessage = null;
			}

			res.json({
				error  : errorMessage,
				success: response,
				jwt    : jwt
			});
		});
	});

	app.post('/auth/logout', function(req, res) {
		var token = req.get('Authorization');
		// If there's a token, we need to get rid of the 'Bearer ' at the beginning
		if(token) {
			token = token.slice(7);
		}

		jwt.revoke(db, req.user, token, function(err) {
			if(err) {
				var errorMessage = err.message;
			} else {
				var errorMessage = null;
			}
			res.json({ error: errorMessage });
		});
	});

	app.post('/auth/register', function(req, res) {

		var user = {
			user     : req.body.user,
			password : <PASSWORD>,
			firstName: req.body.firstName,
			lastName : req.body.lastName,
			gradYear : parseInt(req.body.gradYear)
		};

		if(typeof req.body.teacher !== 'undefined' && req.body.teacher !== false) {
			user.gradYear = null;
		}

		auth.register(db, user, function(err) {
			if(err) {
				var errorMessage = err.message;
			} else {
				var errorMessage = null;
			}
			res.json({ error: errorMessage });
		});
	});

	app.post('/auth/confirm', function(req, res) {
		auth.confirm(db, req.body.user, req.body.hash, function(err) {
			if(err) {
				var errorMessage = err.message;
			} else {
				var errorMessage = null;
			}
			res.end(errorMessage);
		});
	});

}<commit_msg>Make confirmation route return JSON<commit_after>'use strict';

/**
 * @file Manages login API endpoints
 */

var auth = require(__dirname + '/../libs/auth.js');
var jwt  = require(__dirname + '/../libs/jwt.js');

module.exports = function(app, db) {

	app.post('/auth/login', function(req, res) {
		if(req.user) {
			res.json({
				error  : 'You\'re already logged in, silly!',
				success: false,
				jwt    : null
			});
			return;
		}

		var rememberMe = typeof req.body.remember !== 'undefined';

		auth.login(db, req.body.user, req.body.password, rememberMe, function(err, response, jwt) {
			if(err) {
				var errorMessage = err.message;
			} else {
				var errorMessage = null;
			}

			res.json({
				error  : errorMessage,
				success: response,
				jwt    : jwt
			});
		});
	});

	app.post('/auth/logout', function(req, res) {
		var token = req.get('Authorization');
		// If there's a token, we need to get rid of the 'Bearer ' at the beginning
		if(token) {
			token = token.slice(7);
		}

		jwt.revoke(db, req.user, token, function(err) {
			if(err) {
				var errorMessage = err.message;
			} else {
				var errorMessage = null;
			}
			res.json({ error: errorMessage });
		});
	});

	app.post('/auth/register', function(req, res) {

		var user = {
			user     : req.body.user,
			password : <PASSWORD>,
			firstName: req.body.firstName,
			lastName : req.body.lastName,
			gradYear : parseInt(req.body.gradYear)
		};

		if(typeof req.body.teacher !== 'undefined' && req.body.teacher !== false) {
			user.gradYear = null;
		}

		auth.register(db, user, function(err) {
			if(err) {
				var errorMessage = err.message;
			} else {
				var errorMessage = null;
			}
			res.json({ error: errorMessage });
		});
	});

	app.post('/auth/confirm', function(req, res) {
		auth.confirm(db, req.body.user, req.body.hash, function(err) {
			if(err) {
				var errorMessage = err.message;
			} else {
				var errorMessage = null;
			}
			res.json({ error: errorMessage });
		});
	});

}
{'fraction_non_alphanumeric': 0.1682741116751269, 'fraction_numerical': 0.0005076142131979696, 'mean_word_length': 2.796724470134875, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '884418', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1381, 'n_words': 310}