<commit_before>var models = require('../models');
var sequelize = require('../db/sequelize-connection');
var bcrypt = require('bcrypt');

// TODO: Remove globalTeacherCode
var globalTeacherCode = 123;

module.exports = {
  login: function(req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    sequelize.sync().then(function() {
      // search for user in teachers table
      models.teachers.findOne({
        where: {'username': username} 
      })
      .then(function(matchedUser){
        // if there's no match in teachers
        if (!matchedUser) { 
          // search for user in students table
          models.students.findOne({
            where: {'username': username} 
          }).then(function(matchedUser) {
            if (!matchedUser) {
              // ERROR -> client redirects to '/signup'
              res.status(400).send('Invalid user');
            } else {
              // if user is a student, compare stored password with provided password
              bcrypt.compare(password, matchedUser.dataValues.password, function(err, match) {
                if (match) {
                  // Find all classes student is enrolled in (via student_classes table)
                  // Attach them to response object
                  models.students_classes.findAll({
                    where: {'student_id': matchedUser.dataValues.id},
                    include: [models.classes]
                  }).then(function(classes) {
                    var studentObj = {
                      student: {
                        uid: matchedUser.dataValues.id,
                        firstName: matchedUser.dataValues.firstname,
                        lastName: matchedUser.dataValues.lastname,
                        classes: classes.dataValues
                      }
                    };
                    // SUCCESS -> client redirect to '/students'
                    res.status(200).send(studentObj);
                  });
                } else {
                  // ERROR: invalid password -> Client redirects to '/login'
                  res.status(400).send('Invalid password');
                }
              });
            }
          });
        } else {
          // if user is a teacher, compare stored password with provided password
          bcrypt.compare(password, matchedUser.dataValues.password, function(err, match) {
            if (match) {

              // pull teacher's classes from db and attach them to response object
              models.classes.findAll({
                where: {'teacher_id': matchedUser.dataValues.id}
              }).then(function(classes) {
                var teacherObj = {
                  teacher: {
                    uid: matchedUser.dataValues.id,
                    firstName: matchedUser.dataValues.firstname,
                    lastName: matchedUser.dataValues.lastname,
                    classes: classes.dataValues
                  }
                };
              // SUCCESS -> client redirects to '/teachers'
              res.status(200).send(teacherObj);
            });
            } else {
              // ERROR: invalid password -> client redirects to '/login'
              res.status(400).send('Invalid password');
            }
          });
        }
      });
    });
  },

  signup: function(req, res, next) {
    var firstName = req.body.firstName;
    var lastName = req.body.lastName;
    var username = req.body.username;
    var password = <PASSWORD>;
    var accountType = req.body.accountType;

    sequelize.sync().then(function() {
      // if user specifies 'teacher' account, check teachers table
      if (accountType === "teacher") {<commit_msg>Fix 'classes' value bug in teacher & student objects<commit_after>var models = require('../models');
var sequelize = require('../db/sequelize-connection');
var bcrypt = require('bcrypt');

// TODO: Remove globalTeacherCode
var globalTeacherCode = 123;

module.exports = {
  login: function(req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    sequelize.sync().then(function() {
      // search for user in teachers table
      models.teachers.findOne({
        where: {'username': username} 
      })
      .then(function(matchedUser){
        // if there's no match in teachers
        if (!matchedUser) { 
          // search for user in students table
          models.students.findOne({
            where: {'username': username} 
          }).then(function(matchedUser) {
            if (!matchedUser) {
              // ERROR -> client redirects to '/signup'
              res.status(400).send('Invalid user');
            } else {
              // if user is a student, compare stored password with provided password
              bcrypt.compare(password, matchedUser.dataValues.password, function(err, match) {
                if (match) {
                  // Find all classes student is enrolled in (via student_classes table)
                  // Attach them to response object
                  models.students_classes.findAll({
                    where: {'student_id': matchedUser.dataValues.id},
                    include: [models.classes]
                  }).then(function(classes) {
                    var studentObj = {
                      student: {
                        uid: matchedUser.dataValues.id,
                        firstName: matchedUser.dataValues.firstname,
                        lastName: matchedUser.dataValues.lastname,
                        classes: classes
                      }
                    };
                    // SUCCESS -> client redirect to '/students'
                    res.status(200).send(studentObj);
                  });
                } else {
                  // ERROR: invalid password -> Client redirects to '/login'
                  res.status(400).send('Invalid password');
                }
              });
            }
          });
        } else {
          // if user is a teacher, compare stored password with provided password
          bcrypt.compare(password, matchedUser.dataValues.password, function(err, match) {
            if (match) {

              // pull teacher's classes from db and attach them to response object
              models.classes.findAll({
                where: {'teacher_id': matchedUser.dataValues.id}
              }).then(function(classes) {
                var teacherObj = {
                  teacher: {
                    uid: matchedUser.dataValues.id,
                    firstName: matchedUser.dataValues.firstname,
                    lastName: matchedUser.dataValues.lastname,
                    classes: classes
                  }
                };
              // SUCCESS -> client redirects to '/teachers'
              res.status(200).send(teacherObj);
            });
            } else {
              // ERROR: invalid password -> client redirects to '/login'
              res.status(400).send('Invalid password');
            }
          });
        }
      });
    });
  },

  signup: function(req, res, next) {
    var firstName = req.body.firstName;
    var lastName = req.body.lastName;
    var username = req.body.username;
    var password = <PASSWORD>;
    var accountType = req.body.accountType;

    sequelize.sync().then(function() {
      // if user specifies 'teacher' account, check teachers table
      if (accountType === "teacher") {
{'fraction_non_alphanumeric': 0.10569323509711989, 'fraction_numerical': 0.0048225050234427325, 'mean_word_length': 1.5095798319327731, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '4421232', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1760, 'n_words': 457}