<commit_before>var ids = [];
var email;
var gravatar = require('gravatar');

var express = require('express');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('./models/user.js')

module.exports = function(app, io) {

/* MAIN PAGE */
  app.get('/', function(req, res) {

    // Render index.html
    res.render('index');
  });

  app.get('/signout', function(req, res) {
    req.logout();
    // Render index.html
    res.render('index');
  });

  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });

  passport.deserializeUser(function(id, done) {
    User.getUserById(id, function(err, user) {
      done(err, user);
    });
  });

  passport.use(new LocalStrategy({
    usernameField: 'email'
  },
  function(username, password, done) {
   User.getUserByEmail(username, function(err, user){
   	if(err) throw err;
   	if(!user){
      console.log('Unknown user');
   		return done(null, false);
   	}

   	User.comparePassword(password, user.password, function(err, isMatch){
   		if(err) throw err;
   		if(isMatch){
        console.log('User found, Password match');
   			return done(null, user);
   		} else {
        console.log('Invalid password');
   			return done(null, false);
   		}
   	});
   });
  }));

  // Send login form
  app.post('/login',
    passport.authenticate('local', {failureRedirect:'/', failureFlash: 'Invalid username or password.'}),
    function(req, res) {
      console.log("Success")
      res.redirect('/profile');
  });

  // Send registration form
  app.post('/create', function(req,res) {
    var firstname = req.body.f_name;
    var lastname = req.body.l_name;
    var email = req.body.e_mail;
    var password = <PASSWORD>;
    var password2 = <PASSWORD>.<PASSWORD>;

    var newUser = new User({
      email: email,
      password: password,
      firstname: firstname,
      lastname: lastname
    });

    // Validation
  	req.checkBody('f_name', 'Name is required').notEmpty();
    req.checkBody('l_name', 'Name is required').notEmpty();
  	req.checkBody('e_mail', 'Email is required').notEmpty();
  	req.checkBody('e_mail', 'Email is not valid').isEmail();
  	req.checkBody('pwd', 'Password is required').notEmpty();
  	req.checkBody('pwd2', 'Passwords do not match').equals(req.body.pwd);

    var errors = req.validationErrors();

  	if(errors){
  		console.log(errors);
      return res.status(500).send();
		};

    User.createUser(newUser, function(err, user){
			if(err) {throw err;
      console.log('error')}
			console.log(user);
		});

    res.redirect('/');
    return res.status(200).send();
  });

/* USER-PROFILE PAGE */
  app.get('/profile', function(req, res) {

    // Move to user_profile
    res.redirect('/user_profile');
  });

  app.get('/user_profile', function(req, res) {

    // Render user_profile
    res.render('user_profile');
  });

/* TEXT-EDITOR PAGE */
  app.get('/new', function(req, res){<commit_msg>Checks for existing email account<commit_after>var ids = [];
var email;
var gravatar = require('gravatar');

var express = require('express');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('./models/user.js')

module.exports = function(app, io) {

/* MAIN PAGE */
  app.get('/', function(req, res) {

    // Render index.html
    res.render('index');
  });

  app.get('/signout', function(req, res) {
    req.logout();
    // Render index.html
    res.render('index');
  });

  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });

  passport.deserializeUser(function(id, done) {
    User.getUserById(id, function(err, user) {
      done(err, user);
    });
  });

  passport.use(new LocalStrategy({
    usernameField: 'email'
  },
  function(username, password, done) {
   User.getUserByEmail(username, function(err, user){
   	if(err) throw err;
   	if(!user){
      console.log('Unknown user');
   		return done(null, false);
   	}

   	User.comparePassword(password, user.password, function(err, isMatch){
   		if(err) throw err;
   		if(isMatch){
        console.log('User found, Password match');
   			return done(null, user);
   		} else {
        console.log('Invalid password');
   			return done(null, false);
   		}
   	});
   });
  }));

  // Send login form
  app.post('/login',
    passport.authenticate('local', {failureRedirect:'/', failureFlash: 'Invalid username or password.'}),
    function(req, res) {
      console.log("Success")
      res.redirect('/profile');
  });

  // Send registration form
  app.post('/create', function(req,res) {
    var firstname = req.body.f_name;
    var lastname = req.body.l_name;
    var email = req.body.e_mail;
    var password = req.body.pwd;
    var password2 = req.body.<PASSWORD>;

    var newUser = new User({
      email: email,
      password: password,
      firstname: firstname,
      lastname: lastname
    });

    // Validation
  	req.checkBody('f_name', 'Name is required').notEmpty();
    req.checkBody('l_name', 'Name is required').notEmpty();
  	req.checkBody('e_mail', 'Email is required').notEmpty();
  	req.checkBody('e_mail', 'Email is not valid').isEmail();
  	req.checkBody('pwd', 'Password <PASSWORD>').notEmpty();
  	req.checkBody('pwd2', 'Passwords do not match').equals(req.body.pwd);

    var errors = req.validationErrors();

  	if(errors){
  		console.log(errors);
      return res.status(500).send();
		};

    User.findOne({email: email}, function(err, user) {
      if (err) {
        console.log(err);
        return res.status(500).send();
      }
      if (user) {
        console.log('Email exists in system');
        return res.status(500).send();
      }
      else {
        User.createUser(newUser, function(err, user){
    			if(err) {throw err;
          console.log('error')}
    			console.log(user);
    		});
      }
    });

    res.redirect('/');
    return res.status(200).send();
  });

/* USER-PROFILE PAGE */
  app.get('/profile', function(req, res) {

    // Move to user_profile
    res.redirect('/user_profile');
  });

  app.get('/user_profile', function(req, res) {

    // Render user_profile
    res.render('user_profile');
  });

/* TEXT-EDITOR PAGE */
  app.get('/new', function(req, res){
{'fraction_non_alphanumeric': 0.1734970499122947, 'fraction_numerical': 0.0035082124063147823, 'mean_word_length': 3.036036036036036, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '3163999', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2056, 'n_words': 420}