<commit_before>(function() {
  'use strict';
  const User = require('../models/users'),
   // Role = require('../models/roles'),
    Doc = require('../models/documents'),
    config = require('../config/config'),
    jwt = require('jsonwebtoken'),
    us = require('underscore'),
    secretKey = config.secretKey;
  //tokenCreator
  function createToken(user) {
    var token = jwt.sign(user, secretKey, {
      expiresIn: '24h'
    });
    return token;
  }
  
  module.exports = {
    all: function (req, res) {
      User.find({}, function (err, users) {
        if(err) {
          res.send(err);
        } else {
          res.json(users);
        }
      });
    },
    
    register: function(req, res) {
      let newUser = new User(req.body);
      let userData = us.pick(newUser, '_id', 'username', 'name', 'email');
      //create a token for the user
      let token = createToken(userData);
      
      newUser.save(function(err, user){
        if(err) {
          res.send(err);
        } else {
          res.send({
              message: 'User created successfully',
              user: user,
              token: token
            });
        }
      });  
    },

    login: function(req, res, next) {
      //get user from body
      User.findOne({username: req.body.username})
        .select('password')
        .exec(function(err, user) {
        if (err) {
          next(err);
        }
        if(!user) {
          res.send({
            message: 'Wrong username'
          });
        } else if (user) {
          //check password
          let correct = user.checkPass(req.body.password);
          if (!correct) {
            res.status(500).send({
              message: 'Invalid password'
            });
            next(err);
          } else {
            let userData = us.pick(user, '_id', 'username', 'name', 'email'),
              token = createToken(userData);
            res.status(200).send({
              message: 'Login successful',
              token: token,
              user: userData<commit_msg>Make user created have a unique name<commit_after>(function() {
  'use strict';
  const User = require('../models/users'),
   // Role = require('../models/roles'),
    Doc = require('../models/documents'),
    config = require('../config/config'),
    jwt = require('jsonwebtoken'),
    us = require('underscore'),
    secretKey = config.secretKey;
  //tokenCreator
  function createToken(user) {
    var token = jwt.sign(user, secretKey, {
      expiresIn: '24h'
    });
    return token;
  }
  
  module.exports = {
    all: function (req, res) {
      User.find({}, function (err, users) {
        if(err) {
          res.send(err);
        } else {
          res.json(users);
        }
      });
    },
    
    register: function(req, res) {
      User.findOne({username: req.body.username})
        .select('username')
        .exec(function(err, user){
        //check if username is unique
        if (user&&user.username===req.body.username) {
          res.send({
            message: 'Please select another username'
          });
        } else {
          let newUser = new User(req.body);
          let userData = us.pick(newUser, '_id', 'username', 'name', 'email');
          //create a token for the user
          let token = createToken(userData);

          newUser.save(function(err, user){
            if(err) {
              res.send(err);
            } else {
              let userData = us.pick(user, '_id', 'username', 'name', 'email')
              res.send({
                  message: 'User created successfully',
                  user: userData,
                  token: token
                });
            }
          }); 
        }
      });  
    },

    login: function(req, res, next) {
      //get user from body
      User.findOne({username: req.body.username})
        .select('password')
        .exec(function(err, user) {
        if (err) {
          next(err);
        }
        if(!user) {
          res.send({
            message: 'Wrong username'
          });
        } else if (user) {
          //check password
          let correct = user.checkPass(req.body.password);
          if (!correct) {
            res.status(500).send({
              message: 'Invalid password'
            });
            next(err);
          } else {
            let userData = us.pick(user, '_id', 'username', 'name', 'email'),
              token = createToken(userData);
            res.status(200).send({
              message: 'Login successful',
              token: token,
              user: userData
{'fraction_non_alphanumeric': 0.1462192198736108, 'fraction_numerical': 0.003486598387448246, 'mean_word_length': 1.5714285714285714, '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': '4609161', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1299, 'n_words': 297}