<commit_before>var express = require('express');
var controller = express.Router();
var User = require('../models/Users');
var Post = require('../models/Posts');
var bcrypt = require('bcrypt');
var Salt = bcrypt.genSaltSync(10);

controller.get('/', function(req, res, next) {
  res.send('It works!');
});

// logout
controller.get('/logout', function(req, res, next) {
  // destroy session
        console.log(req.session.user)
  req.session.user = null;
        console.log(req.session.user)
  res.json({ 'message': 'You have been logged out.'});
});

// create a account
controller.post('/signup', function(req, res, next){
  var userInfo = {
    username: req.body.username,
    email: req.body.email,
    password: <PASSWORD>(req.body.password, Salt)
  };
  User.find({ /*username: userInfo.username,*/ email: userInfo.email }, function(err, users) {
    console.log(users.length);
    if (users.length >= 1) {
      res.json({ 'message': 'This account and/or email already exists!'})
    } else if (users.length === 0 || (users.length === 1 && users[0].username !== userInfo.username || users[0].email !== userInfo.email)) {
      User.create(userInfo, function(err, users) {
        // Look at this =====================================
        // req.session.user = users[0].email;
        // ==================================================
        res.json({ 'message': 'You have successfully registered an account!'})
      });
    } else {
      res.json({'message': 'error'})
    }
  })
});

// Login
controller.post('/login', function(req, res, next) {
  var userInfo = {
    email: req.body.email,
    password: <PASSWORD>
  };
  User.find({ email: userInfo.email }, function(err, user) {
    var isPasswordValid = bcrypt.compareSync(userInfo.password, user[0].password);
    if (isPasswordValid) {
            console.log(req.session.user)
      req.session.user = user[0].email;
      // delete this after use
      console.log(req.session.user)
      res.json({ 'message': 'Logged in successfully'});
    } else {
      res.json({ 'message': 'Invalid username and/or password'});
    }
  });
});

// update
controller.put('/update', function(req, res) {
  var userInfo = {
    username: req.body.username,
    email: req.body.email,
    password: <PASSWORD>.hashSync(req.body.password, Salt)
  };
  User.findOneAndUpdate({ email: req.session.user }, userInfo, function (err, users) {
    if (err) console.log(err);
    console.log(userInfo.email);
    console.log(req.session.user)
    // fix this ======================================================
    req.session.user = userInfo.email;
    console.log(req.session.user);
    res.json({ 'message': 'Account has been updated' })
  })
})

// Not sure about this????
// Delete
controller.delete('/delete/:id', function(req, res) {
  User.findByIdAndRemove(req.params.id, req.body, function(err, user) {
    if (err) console.log(err);
    res.json({ 'message': 'Account has been deleted'});
  });
});

// DELETE: work on this
// controller.delete('/delete', function(req, res) {
//   var userInfo = {
//     username: req.body.username,
//     email: req.body.email,
//     password:
//   }
//   User.findOneAndRemove({ })
// })

module.exports = controller;<commit_msg>deletes users account using  sessions<commit_after>var express = require('express');
var controller = express.Router();
var User = require('../models/Users');
var Post = require('../models/Posts');
var bcrypt = require('bcrypt');
var Salt = bcrypt.genSaltSync(10);

controller.get('/', function(req, res, next) {
  res.send('It works!');
});

// logout
controller.get('/logout', function(req, res, next) {
  // destroy session
  req.session.user = null;
  res.json({ 'message': 'You have been logged out.'});
});

// create a account
controller.post('/signup', function(req, res, next){
  var userInfo = {
    username: req.body.username,
    email: req.body.email,
    password: <PASSWORD>.hashSync(req.body.password, Salt)
  };
  User.find({ /*username: userInfo.username,*/ email: userInfo.email }, function(err, users) {
    console.log(users.length);
    if (users.length >= 1) {
      res.json({ 'message': 'This account and/or email already exists!'})
    } else if (users.length === 0 || (users.length === 1 && users[0].username !== userInfo.username || users[0].email !== userInfo.email)) {
      User.create(userInfo, function(err, users) {
        // Look at this =====================================
        // req.session.user = users[0].email;
        // ==================================================
        res.json({ 'message': 'You have successfully registered an account!'})
      });
    } else {
      res.json({'message': 'error'})
    }
  })
});

// Login
controller.post('/login', function(req, res, next) {
  var userInfo = {
    email: req.body.email,
    password: <PASSWORD>
  };
  User.find({ email: userInfo.email }, function(err, user) {
    var isPasswordValid = bcrypt.compareSync(userInfo.password, user[0].password);
    if (isPasswordValid) {
      req.session.user = user[0].email;
      // delete this after use
      res.json({ 'message': 'Logged in successfully'});
    } else {
      res.json({ 'message': 'Invalid username and/or password'});
    }
  });
});

// update
controller.put('/update', function(req, res) {
  var userInfo = {
    username: req.body.username,
    email: req.body.email,
    password: <PASSWORD>.hashSync(req.body.password, Salt)
  };
  User.findOneAndUpdate({ email: req.session.user }, userInfo, function (err, users) {
    if (err) console.log(err);
    req.session.user = userInfo.email;
    res.json({ 'message': 'Account has been updated' })
  })
})

// DELETE
controller.delete('/delete', function(req, res) {
  User.findOneAndRemove({ email: req.session.user }, req.session, function(err, user) {
    if (err) console.log(err);
    res.json({ 'message': 'Account has been deleted' })
  })
})

module.exports = controller;
{'fraction_non_alphanumeric': 0.2121823994615514, 'fraction_numerical': 0.003365303718660609, 'mean_word_length': 3.383480825958702, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '1416531', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1828, 'n_words': 421}