<commit_before>// Creates new password reset token and send it to user's email.


'use strict';


var _         = require('lodash');
var url       = require('url');
var recaptcha = require('nodeca.core/lib/recaptcha');


module.exports = function (N, apiPath) {
  N.validate(apiPath, {
    email:                     { type: 'string', required: true }
  , recaptcha_challenge_field: { type: 'string', required: true }
  , recaptcha_response_field:  { type: 'string', required: true }
  });

  N.wire.on(apiPath, function (env, callback) {
    var privateKey = N.config.options.recaptcha.private_key
      , clientIp   = env.request.ip
      , challenge  = env.params.recaptcha_challenge_field
      , response   = env.params.recaptcha_response_field;

    recaptcha.verify(privateKey, clientIp, challenge, response, function (err, valid) {
      if (err || !valid) {
        callback({
          code:    N.io.CLIENT_ERROR
        , message: env.t('wrong_captcha_response')
        });
        return;
      }

      N.models.users.AuthLink.findOne({
        'providers.email': env.params.email
      , 'providers.type': 'plain'
      }, function (err, authlink) {
        if (err) {
          callback(err);
          return;
        }

        if (!authlink) {
          callback({
            code:    N.io.NOT_FOUND
          , message: env.t('unknown_email')
          });
          return;
        }

        var provider = _.find(authlink.providers, {
          type: 'plain'
        , email: env.params.email
        });

        // Must not happen but it's better to check.
        if (!provider) {
          callback({
            code:    N.io.NOT_FOUND
          , message: env.t('unknown_email')
          });
          return;
        }

        N.models.users.TokenResetPassword.create({
          authlink_id:     authlink._id
        , authprovider_id: provider._id
        }, function (err, token) {
          N.settings.get('general_project_name', {}, function (err, projectName) {
            var link;

            // Construct base link.
            link = N.runtime.router.linkTo('users.auth.reset_password.apply_form', {
              secret_key: token.secret_key
            });

            // Prepend protocol and host if link not contains them.
            link = url.resolve(env.origin.req.fullUrl, link);

            N.mailer.send({
              to:      provider.email
            , subject: env.t('confirmation_email_subject', { project_name: projectName })
            , text:    env.t('confirmation_email_text',    { link: link })
            }, callback);
          });
        });
      });
    });
  });
};
<commit_msg>Remove unreachable code in reset_password/request.js<commit_after>// Creates new password reset token and send it to user's email.


'use strict';


var _         = require('lodash');
var url       = require('url');
var recaptcha = require('nodeca.core/lib/recaptcha');


module.exports = function (N, apiPath) {
  N.validate(apiPath, {
    email:                     { type: 'string', required: true }
  , recaptcha_challenge_field: { type: 'string', required: true }
  , recaptcha_response_field:  { type: 'string', required: true }
  });

  N.wire.on(apiPath, function (env, callback) {
    var privateKey = N.config.options.recaptcha.private_key
      , clientIp   = env.request.ip
      , challenge  = env.params.recaptcha_challenge_field
      , response   = env.params.recaptcha_response_field;

    recaptcha.verify(privateKey, clientIp, challenge, response, function (err, valid) {
      if (err || !valid) {
        callback({
          code:    N.io.CLIENT_ERROR
        , message: env.t('wrong_captcha_response')
        });
        return;
      }

      N.models.users.AuthLink.findOne({
        'providers.email': env.params.email
      , 'providers.type': 'plain'
      }, function (err, authlink) {
        if (err) {
          callback(err);
          return;
        }

        if (!authlink) {
          callback({
            code:    N.io.NOT_FOUND
          , message: env.t('unknown_email')
          });
          return;
        }

        var provider = _.find(authlink.providers, {
          type: 'plain'
        , email: env.params.email
        });

        N.models.users.TokenResetPassword.create({
          authlink_id:     authlink._id
        , authprovider_id: provider._id
        }, function (err, token) {
          N.settings.get('general_project_name', {}, function (err, projectName) {
            var link;

            // Construct base link.
            link = N.runtime.router.linkTo('users.auth.reset_password.apply_form', {
              secret_key: token.secret_key
            });

            // Prepend protocol and host if link not contains them.
            link = url.resolve(env.origin.req.fullUrl, link);

            N.mailer.send({
              to:      provider.email
            , subject: env.t('confirmation_email_subject', { project_name: projectName })
            , text:    env.t('confirmation_email_text',    { link: link })
            }, callback);
          });
        });
      });
    });
  });
};

{'fraction_non_alphanumeric': 0.12643678160919541, 'fraction_numerical': 0.0, 'mean_word_length': 1.8778026905829597, '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': '4031940', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1484, 'n_words': 306}