<commit_before>'use strict';
var async = require('async');

exports.name = 'kabam-core-api';
exports.routes = function(kernel){
  //registration by username, password and email
  //todo - implement the https://npmjs.org/packages/captcha
  kernel.app.post('/auth/signup', function (request, response) {
    request.model.User.signUp(request.body.username, request.body.email, request.body.password,
      function (err, userCreated) {
        if (request.is('json')) {
          if (err) {
            response.json(400, {
              errors: {form: err.message}
            });
          } else {
            userCreated.notify('email', {'subject': 'Account confirmation', 'template': 'signin'});
            response.json(201, {
              'username': userCreated.username,
              'email': userCreated.email
            });
          }
        } else {
          if (err) {
            request.flash('error', err.message);
          } else {
            userCreated.notify('email', {'subject': 'Account confirmation', 'template': 'signin'});
            request.flash('info', 'You have been registered! Please, check your email for instructions!');
          }
          response.redirect('/');
        }
      }
    );
  });

  //verify that login/email is not busy! - to be called by ajax
  kernel.app.post('/auth/isBusy', function (request, response) {
    async.parallel({
      'username': function (cb) {
        var username = request.body.username;
        if (username) {
          if (/^[a-zA-Z0-9_]{3,32}$/.test(username)) { //username looks OK
            request.model.User.findOne({'username': username}, function (err, userFound) {
              cb(err, (userFound ? 'USERNAME_BUSY' : 'OK'));
            });
          } else {
            cb(null, 'BAD_USERNAME');//username looks bad
          }
        } else {
          cb(null, 'EMPTY_USERNAME');//username is empty
        }
      },
      'email': function (cb) {
        var email = request.body.email;
        if (email) {
          // https://github.com/chriso/node-validator/blob/master/lib/validators.js#L27
          if (/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/.test(email)) { //email looks OK
            request.model.User.findOne({'email': email}, function (err, userFound) {
              cb(err, (userFound ? 'EMAIL_BUSY' : 'OK'));
            });
          } else {
            cb(null, 'BAD_EMAIL');//email looks bad
          }
        } else {
          cb(null, 'EMPTY_EMAIL');//email is empty
        }
      }
    }, function (err, results) {
      if (err) {
        throw err;
      }
      setTimeout(function () {
        response.json(results);
      }, Math.floor(Math.random() * 1000));//to make sad time based analysis that try to guess user's accounts
    });
  });

  kernel.app.post('/auth/profile',function (request, response){
    if(!request.user){return response.send(401);}

    var fields =['firstName', 'lastName', 'skype'];
    fields.map(function(p){
      // jshint expr: true
      request.body[p] && (request.user[p] = request.body[p]);
    });


    // password change: if we have a new password we should verify old first
    if(request.body.newPassword){
      // if user doesn't have a password yet (signed up using oauth) then we don't need to check password
      // but if they have, they definitely we have to<commit_msg>feat(core/api): route get current user profile<commit_after>'use strict';
var async = require('async');

exports.name = 'kabam-core-api';
exports.routes = function(kernel){
  //registration by username, password and email
  //todo - implement the https://npmjs.org/packages/captcha
  kernel.app.post('/auth/signup', function (request, response) {
    request.model.User.signUp(request.body.username, request.body.email, request.body.password,
      function (err, userCreated) {
        if (request.is('json')) {
          if (err) {
            response.json(400, {
              errors: {form: err.message}
            });
          } else {
            userCreated.notify('email', {'subject': 'Account confirmation', 'template': 'signin'});
            response.json(201, {
              'username': userCreated.username,
              'email': userCreated.email
            });
          }
        } else {
          if (err) {
            request.flash('error', err.message);
          } else {
            userCreated.notify('email', {'subject': 'Account confirmation', 'template': 'signin'});
            request.flash('info', 'You have been registered! Please, check your email for instructions!');
          }
          response.redirect('/');
        }
      }
    );
  });

  //verify that login/email is not busy! - to be called by ajax
  kernel.app.post('/auth/isBusy', function (request, response) {
    async.parallel({
      'username': function (cb) {
        var username = request.body.username;
        if (username) {
          if (/^[a-zA-Z0-9_]{3,32}$/.test(username)) { //username looks OK
            request.model.User.findOne({'username': username}, function (err, userFound) {
              cb(err, (userFound ? 'USERNAME_BUSY' : 'OK'));
            });
          } else {
            cb(null, 'BAD_USERNAME');//username looks bad
          }
        } else {
          cb(null, 'EMPTY_USERNAME');//username is empty
        }
      },
      'email': function (cb) {
        var email = request.body.email;
        if (email) {
          // https://github.com/chriso/node-validator/blob/master/lib/validators.js#L27
          if (/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/.test(email)) { //email looks OK
            request.model.User.findOne({'email': email}, function (err, userFound) {
              cb(err, (userFound ? 'EMAIL_BUSY' : 'OK'));
            });
          } else {
            cb(null, 'BAD_EMAIL');//email looks bad
          }
        } else {
          cb(null, 'EMPTY_EMAIL');//email is empty
        }
      }
    }, function (err, results) {
      if (err) {
        throw err;
      }
      setTimeout(function () {
        response.json(results);
      }, Math.floor(Math.random() * 1000));//to make sad time based analysis that try to guess user's accounts
    });
  });

  kernel.app.get('/auth/profile', function(req, res){
    if(!req.user){return res.json(401, {message: 'Unauthorized'});}
    res.json(200, req.user.export());
  });

  kernel.app.post('/auth/profile',function (request, response){
    if(!request.user){return response.send(401);}

    var fields =['firstName', 'lastName', 'skype'];
    fields.map(function(p){
      // jshint expr: true
      request.body[p] && (request.user[p] = request.body[p]);
    });


    // password change: if we have a new password we should verify old first
    if(request.body.newPassword){
      // if user doesn't have a password yet (signed up using oauth) then we don't need to check password
      // but if they have, they definitely we have to
{'fraction_non_alphanumeric': 0.2009698275862069, 'fraction_numerical': 0.017241379310344827, 'mean_word_length': 2.1475201356506997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '5057368', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2371, 'n_words': 481}