<commit_before>'use strict';

/**
 * Module dependencies.
 */
var path = require('path'),
    errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
    analyticsHandler = require(path.resolve('./modules/core/server/controllers/analytics.server.controller')),
    emailService = require(path.resolve('./modules/core/server/services/email.server.service')),
    async = require('async'),
    crypto = require('crypto'),
    mongoose = require('mongoose'),
    User = mongoose.model('User');

/**
 * Forgot for reset password (forgot POST)
 */
exports.forgot = function(req, res, next) {
  async.waterfall([
    // Generate random token
    function(done) {
      crypto.randomBytes(20, function(err, buffer) {
        var token = buffer.toString('hex');
        done(err, token);
      });
    },
    // Lookup user by username
    function(token, done) {
      if (req.body.username) {
        var username = req.body.username.toString().toLowerCase();
        User.findOne({
          $or: [
            { username: username.toLowerCase() },
            { email: username.toLowerCase() }
          ]
        }, '-salt -password', function(err, user) {
          if (!user) {
            return res.status(404).send({
              message: 'We could not find an account with that username or email. Make sure you have it spelled correctly.'
            });
          } else {
            user.resetPasswordToken = <PASSWORD>;
            user.resetPasswordExpires = Date.now() + (24 * 3600000); // 24 hours

            user.save(function(err) {
              done(err, user);
            });
          }
        });
      } else {
        return res.status(400).send({
          message: 'Please, we really need your username or email first...'
        });
      }
    },

    // Send email
    function(user) {
      emailService.sendResetPassword(user, function(err) {
        if (!err) {
          return res.send({
            message: 'Password reset sent.'
          });
        } else {
          return res.status(400).send({
            message: 'Failure while sending recovery email to you. Please try again later.'
          });
        }
      });
    }

  ], function(err) {
    if (err) return next(err);
  });<commit_msg>Add stats to password reset<commit_after>'use strict';

/**
 * Module dependencies.
 */
var path = require('path'),
    errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
    analyticsHandler = require(path.resolve('./modules/core/server/controllers/analytics.server.controller')),
    emailService = require(path.resolve('./modules/core/server/services/email.server.service')),
    statService = require(path.resolve('./modules/stats/server/services/stats.server.service')),
    async = require('async'),
    crypto = require('crypto'),
    mongoose = require('mongoose'),
    User = mongoose.model('User');

/**
 * Forgot for reset password (forgot POST)
 */
exports.forgot = function(req, res, next) {
  async.waterfall([

    // Generate random token
    function(done) {
      crypto.randomBytes(20, function(err, buffer) {
        var token = buffer.toString('hex');
        done(err, token);
      });
    },

    // Lookup user by username
    function(token, done) {

      // Missing username, return error
      if (!req.body.username) {
        return res.status(400).send({
          message: 'Please, we really need your username or email first...'
        });
      }

      var userHandle = req.body.username.toString().toLowerCase();

      User.findOne({
        $or: [
          { username: userHandle },
          { email: userHandle }
        ]
      }, '-salt -password', function(err, user) {
        if (!user) {

          // Report failure to reset to stats
          return statService.stat({
            namespace: 'passwordReset',
            counts: {
              count: 1
            },
            tags: {
              status: 'failed:noUser'
            }
          }, function() {
            // Return failure
            res.status(404).send({
              message: 'We could not find an account with that username or email. Make sure you have it spelled correctly.'
            });
          });

        } else {
          user.resetPasswordToken = <PASSWORD>;
          user.resetPasswordExpires = Date.now() + (24 * 3600000); // 24 hours

          user.save(function(err) {
            done(err, user);
          });
        }
      });

    },

    // Send email
    function(user) {
      emailService.sendResetPassword(user, function(err) {

        // Stop on errors
        if (err) {
          return res.status(400).send({
            message: 'Failure while sending recovery email to you. Please try again later.'
          });
        }

        // Report successfull reset to stats
        return statService.stat({
          namespace: 'passwordReset',
          counts: {
            count: 1
          },
          tags: {
            status: 'emailSent'
          }
        }, function() {
          // Return success
          res.send({
            message: 'Password reset sent.'
          });
        });

      });
    }

  ], function(err) {
    if (err) {
      return next(err);
    }
  });
{'fraction_non_alphanumeric': 0.13745509548118737, 'fraction_numerical': 0.008697296275288334, 'mean_word_length': 1.8875545851528384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4672286', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1413, 'n_words': 338}