<commit_before>/* jshint node: true */
'use strict';

var LocalStrategy = require('passport-local').Strategy;
var helpers = require('../routes/helpers');

// load user model
var User = require('../models/user');
var Token = require('../models/token');

var Access = require('./access');

module.exports = function (passport) {

	// =========================================================================
	// passport session setup ==================================================
	// =========================================================================
	// required for persistent login sessions
	// passport needs ability to serialize and unserialize users out of session

	// used to serialize the user for the session
	passport.serializeUser(function (user, done) {
		done(null, user.id);
	});

	// used to deserialize the user
	passport.deserializeUser(function (id, done) {
		User.findById(id, function (err, user) {
			done(err, user);
		});
	});

	// =========================================================================
	// LOCAL SIGNUP ============================================================
	// =========================================================================
	// we are using named strategies since we have one for login and one for signup

	passport.use('local-signup', new LocalStrategy({
		// by default, local strategy uses username and password,
		// we will override with email
		usernameField: 'email',
		passwordField: 'password',

		// allows us to pass back the entire request to the callback
		passReqToCallback: true,
	},
    function (req, email, password, done) {
		email = email.toLowerCase();

		// asynchronous
		// User.findOne wont fire unless data is sent back
		process.nextTick(function () {
			Token.findOne({ token: req.body.token, email: email, tokenType: true },
				function (err, token) {
					if (err) {
						console.log(err);
					}

					// invalid token
					if (token) {
						// find a user whose email is the same as the forms email
						// we are checking to see if the user trying to login already exists
						User.findOne({ email: email }, function (err, user) {
							// if there are any errors, return the error
							if (err) {
								return done(err);
							}

							// check to see if theres already a user with that email
							if (user) {
								req.session.submission = req.body;
								return done(null, false, req.flash('registerFlash',
										{
											text: 'That email is already taken.',
											class: 'danger',
										}));
							} else if (password != req.body.password2) {
								req.session.submission = req.body;
								return done(null, false, req.flash('registerFlash',
										{
											text: 'Those passwords do not match.',
											class: 'danger',
										}));
							} else {
								// if there is no user with that email
								// create the user
								var newUser = new User();

								// set the user's local credentials
								newUser.email = email;

								// This password will be hashed, and in the process
								// overwrite the plain text password we just stored into newUser.hash
								newUser.hash = password;
								newUser.name = token.name;
								newUser.phone = req.body.phone;
								newUser.access = Access.VOLUNTEER;
								newUser.countryCode = token.country;

								// save the user
								newUser.save(function (err) {
									if (err) {
										return done(err);
									}

									var sendFrom = 'Pe<NAME> <<EMAIL>>';
									var sendTo = email;
									var subject = 'Peace Corps BonVoyage Registration Confirmation';
									var map = {
										name: token.name.split(' ')[0],
										button: 'http://localhost:3000',
									};

									// asynchronous
									process.nextTick(function () {
										helpers.sendTemplateEmail(sendFrom, sendTo, subject,
										'welcome', map);
									});

									return done(null, newUser);
								});

								token.remove();
							}
						});
					} else {
						return done(null, false, req.flash('registerFlash',
							{
								text: 'Registration Token is invalid.',
								class: 'danger',
							}));
					}
				});
		});<commit_msg>passport flash message is updated<commit_after>/* jshint node: true */
'use strict';

var LocalStrategy = require('passport-local').Strategy;
var helpers = require('../routes/helpers');

// load user model
var User = require('../models/user');
var Token = require('../models/token');

var Access = require('./access');

module.exports = function (passport) {

	// =========================================================================
	// passport session setup ==================================================
	// =========================================================================
	// required for persistent login sessions
	// passport needs ability to serialize and unserialize users out of session

	// used to serialize the user for the session
	passport.serializeUser(function (user, done) {
		done(null, user.id);
	});

	// used to deserialize the user
	passport.deserializeUser(function (id, done) {
		User.findById(id, function (err, user) {
			done(err, user);
		});
	});

	// =========================================================================
	// LOCAL SIGNUP ============================================================
	// =========================================================================
	// we are using named strategies since we have one for login and one for signup

	passport.use('local-signup', new LocalStrategy({
		// by default, local strategy uses username and password,
		// we will override with email
		usernameField: 'email',
		passwordField: 'password',

		// allows us to pass back the entire request to the callback
		passReqToCallback: true,
	},
    function (req, email, password, done) {
		email = email.toLowerCase();

		// asynchronous
		// User.findOne wont fire unless data is sent back
		process.nextTick(function () {
			Token.findOne({ token: req.body.token, email: email, tokenType: true },
				function (err, token) {
					if (err) {
						console.log(err);
					}

					// invalid token
					if (token) {
						// find a user whose email is the same as the forms email
						// we are checking to see if the user trying to login already exists
						User.findOne({ email: email }, function (err, user) {
							// if there are any errors, return the error
							if (err) {
								return done(err);
							}

							// check to see if theres already a user with that email
							if (user) {
								req.session.submission = req.body;
								return done(null, false, req.flash('loginFlash',
										{
											text: 'That email is already taken.',
											class: 'danger',
										}));
							} else if (password != req.body.password2) {
								req.session.submission = req.body;
								return done(null, false, req.flash('loginFlash',
										{
											text: 'Those passwords do not match.',
											class: 'danger',
										}));
							} else {
								// if there is no user with that email
								// create the user
								var newUser = new User();

								// set the user's local credentials
								newUser.email = email;

								// This password will be hashed, and in the process
								// overwrite the plain text password we just stored into newUser.hash
								newUser.hash = password;
								newUser.name = token.name;
								newUser.phone = req.body.phone;
								newUser.access = Access.VOLUNTEER;
								newUser.countryCode = token.country;

								// save the user
								newUser.save(function (err) {
									if (err) {
										return done(err);
									}

									var sendFrom = '<NAME> <<EMAIL>>';
									var sendTo = email;
									var subject = 'Peace Corps BonVoyage Registration Confirmation';
									var map = {
										name: token.name.split(' ')[0],
										button: 'http://localhost:3000',
									};

									// asynchronous
									process.nextTick(function () {
										helpers.sendTemplateEmail(sendFrom, sendTo, subject,
										'welcome', map);
									});

									return done(null, newUser);
								});

								token.remove();
							}
						});
					} else {
						return done(null, false, req.flash('loginFlash',
							{
								text: 'Registration Token is invalid.',
								class: 'danger',
							}));
					}
				});
		});
{'fraction_non_alphanumeric': 0.20384294068504594, 'fraction_numerical': 0.0014321518080916578, 'mean_word_length': 2.444307439375257, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1023720', 'n_tokens_mistral': 3252, 'n_tokens_neox': 2155, 'n_words': 704}