<commit_before>'use strict';
var _ = require('lodash');

function getConfig() {
	var found = null;
	_.forEach(sails.config.waterlock.authMethod, function(method) {
		if (method.name === 'waterlock-spotify-auth') {
			found = method;
		}
	});
	return found;
}

module.exports = function(req, res) {
	var params = req.params.all();

	this.spotify.tokenExchange(params.code, accessTokenResponse);

	function accessTokenResponse(error, result) {
		if (error || typeof result === 'undefined') {
			if (error.status) {
				return res.status(error.status)
					.json(error);
			} else {
				return res.serverError(error);
			}
		}

		var attr = {
			spotifyEmail: result.body.email
		};

		var c = getConfig();
		var fieldMap = c.fieldMap || {};
		_.each(fieldMap, function(val, key) {
				if (!_.isUndefined(result.body[val])) {
					attr[key] = result.body[val];
				}
		});

		if (req.session.authenticated) {
			attr['user'] = req.session.user.id;
			waterlock.engine.attachAuthToUser(attr, req.session.user, userFound);
		} else {
			waterlock.engine.findOrCreateAuth(attr, attr, userFound);
		}
	}

	function userFound(err, user) {
		if (err) {
			waterlock.logger.debug(err);
			return waterlock.cycle.loginFailure(req, res, null, {
				error: 'trouble creating model'
			});
		}

		waterlock.cycle.loginSuccess(req, res, user);
	}
};<commit_msg>update to n*1 auth<commit_after>'use strict';
var _ = require('lodash');

function getConfig() {
	var found = null;
	_.forEach(sails.config.waterlock.authMethod, function(method) {
		if (method.name === 'waterlock-spotify-auth') {
			found = method;
		}
	});
	return found;
}

module.exports = function(req, res) {
	var params = req.params.all();

	this.spotify.tokenExchange(params.code, accessTokenResponse);

	function accessTokenResponse(error, result) {
		if (error || typeof result === 'undefined') {
			if (error.status) {
				return res.status(error.status)
					.json(error);
			} else {
				return res.serverError(error);
			}
		}

		var attr = {
			spotifyEmail: result.body.email,
			provider: 'spotify'
		};

		var c = getConfig();
		var fieldMap = c.fieldMap || {};
		_.each(fieldMap, function(val, key) {
				if (!_.isUndefined(result.body[val])) {
					attr[key] = result.body[val];
				}
		});

		if (req.session.authenticated) {
			attr['user'] = req.session.user.id;
			waterlock.engine.attachAuthToUser(attr, req.session.user, userFound);
		} else {
			waterlock.engine.findOrCreateAuth({email: attr.email, provider: 'spotify'}, attr, userFound);
		}
	}

	function userFound(err, user) {
		if (err) {
			waterlock.logger.debug(err);
			return waterlock.cycle.loginFailure(req, res, null, {
				error: 'trouble creating model'
			});
		}

		waterlock.cycle.loginSuccess(req, res, user);
	}
};
{'fraction_non_alphanumeric': 0.15995647442872687, 'fraction_numerical': 0.0003627130939426913, 'mean_word_length': 3.6509274873524453, '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': '4533245', 'n_tokens_mistral': 1091, 'n_tokens_neox': 946, 'n_words': 182}