<commit_before>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    User = mongoose.model('User'),
    Token = mongoose.model('LoginToken'),
    Account = mongoose.model('Account'),
    moment = require('moment'),
    jwt = require('jwt-simple'),
    config = require('meanio').loadConfig(),
    mandrill = require('mandrill-api/mandrill'),
    mandrill_client = new mandrill.Mandrill(config.mandrill.key)
;

/**
 * Send logged in User account
 */
exports.user = function(req, res) {

  // get user
  if (req.user && req.user._id) {
    User
      .findById(req.user._id)
      .populate('accounts')
      .exec(function(err,user) {
        console.log('user', user);
        console.log('err', err);

        return res.json(user);
      });
  } else {
    console.log('returning user', req.user);
    return res.json(req.user || null);
  }
};


/**
 * Login a user with an email-address and send a token
 */
exports.login = function(req,res,next){

  var email = req.body.email;
  // Check if email-address is registered
  User.findOne({'email': email}, function(err,user){
    if (err) {
      res.json({ status: 'error', error: err });
    } else if(!user){
      res.json({ status: 'error', error: 'User not found' });
    } else {

      // User found, create a new token
      var token = new Token();
      token.user = user._id;
      token.save(function(err) { if (err) { res.json({ status: 'error', error: err }); } });

      // Send the new token by email
      mandrill_client.messages.send({
        'message': {
          'html': req.protocol + '://' + req.get('host') + '/#!/u/signin?token=' + token.token,
          'text': req.protocol + '://' + req.get('host') + '/#!/u/signin?token=' + token.token,
          'subject': 'Login at Columby',
          'from_email': '<EMAIL>',
          'from_name': '<NAME>',
          'to': [{
            'email': user.email,
            'name': user.name,
            'type': 'to'
          }],
          'headers': {
            'Reply-To': '<EMAIL>'
          },
        }
      }, function(result){
        if (result[0].status === 'sent') {
          res.json({ status: 'success', statusMessage: 'VerificationToken sent.' });
        } else {
          res.json({ status: 'error', err: 'Error sending mail.' });
        }
      });
    }
  });
};
<commit_msg>Email template integration<commit_after>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    User = mongoose.model('User'),
    Token = mongoose.model('LoginToken'),
    Account = mongoose.model('Account'),
    moment = require('moment'),
    jwt = require('jwt-simple'),
    config = require('meanio').loadConfig(),
    mandrill = require('mandrill-api/mandrill'),
    mandrill_client = new mandrill.Mandrill(config.mandrill.key)
;

/**
 * Send logged in User account
 */
exports.user = function(req, res) {

  // get user
  if (req.user && req.user._id) {
    User
      .findById(req.user._id)
      .populate('accounts')
      .exec(function(err,user) {
        console.log('user', user);
        console.log('err', err);

        return res.json(user);
      });
  } else {
    console.log('returning user', req.user);
    return res.json(req.user || null);
  }
};


/**
 * Login a user with an email-address and send a token
 */
exports.login = function(req,res,next){

  var email = req.body.email;
  // Check if email-address is registered
  User.findOne({'email': email}, function(err,user){
    if (err) {
      res.json({ status: 'error', error: err });
    } else if(!user){
      res.json({ status: 'error', error: 'User not found' });
    } else {

      // User found, create a new token
      var token = new Token();
      token.user = user._id;
      token.save(function(err) { if (err) { res.json({ status: 'error', error: err }); } });

      //create token url
      var tokenurl = req.protocol + '://' + req.get('host') + '/#!/u/signin?token=' + token.token;

      // Send the new token by email
      mandrill_client.messages.sendTemplate({
        'template_name': 'columby-notice-template',
        'template_content' : [{
          'name' : 'example name',
          'content' : 'example content'
        }],
        'message': {
          'html': req.protocol + '://' + req.get('host') + '/#!/u/signin?token=' + token.token,
          'text': req.protocol + '://' + req.get('host') + '/#!/u/signin?token=' + token.token,
          'subject': 'Login at Columby',
          'from_email': '<EMAIL>',
          'from_name': '<NAME>',
          'to': [{
            'email': user.email,
            'name': user.name,
            'type': 'to'
          }],
          'headers': {
            'Reply-To': '<EMAIL>'
          },
          'merge_vars': [{
            'rcpt' : user.email,
            'vars': [{
              'name':'TITLE',
              'content':'Your login token',
            },{
              'name':'message',
              'content':'Hi, ' + user.name + '!<br/>There was a request to login. Please use this url to login<br><br>' + tokenurl + '<br><br>If you did not make this request, just ignore this email.'
            }],
          }],
        }
      }, function(result){
        if (result[0].status === 'sent') {
          res.json({ status: 'success', statusMessage: 'VerificationToken sent.' });
        } else {
          res.json({ status: 'error', err: 'Error sending mail.' });
        }
      });
    }
  });
};

{'fraction_non_alphanumeric': 0.18501557061732918, 'fraction_numerical': 0.00036636746656896867, 'mean_word_length': 2.178114086146682, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '4759722', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1706, 'n_words': 391}