{"size": 1520, "ext": "js", "max_stars_repo_path": "back/config/passport/signup.js", "max_stars_repo_name": "Ibanheiz/mean-template", "max_stars_repo_stars_event_min_datetime": "2015-10-20T19:09:12.000Z", "max_stars_repo_stars_event_max_datetime": "2015-10-27T18:05:08.000Z", "max_issues_repo_path": "back/config/passport/signup.js", "max_issues_repo_name": "Ibanheiz/mean-seed", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "back/config/passport/signup.js", "max_forks_repo_name": "Ibanheiz/mean-seed", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 9, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.7777777778, "max_line_length": 84, "alphanum_fraction": 0.5690789474}
module.exports = function (passport, User) {
  var LocalStrategy   = require('passport-local').Strategy;
  var bCrypt = require('bcrypt-nodejs');

  var criptografar = function (password) {
    return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
  };

  passport.use('signup', new LocalStrategy({
    passReqToCallback : true
  },
    function (req, username, password, done) {
      username = req.body.username;
      password = req.body.password;
      var email = req.body.email;
      var findOrCreateUser = function () {
        User.findOne({ 'username' :  username }, function (err, user) {
          if (err) {
            console.log('Error no Signup: ' + err);
            return done(err);
          }
          if (user) {
            console.log('Já existe o usuário: ' + username);
            return done(null, false, {message: 'O usuário já existe'});
          }
          var newUser = new User();
          newUser.username = username;
          newUser.password = criptografar(password);
          newUser.email = email;

          newUser.save(function (err) {
            if (err) {
              console.log('Erro ao salvar um novo usuário: ' + err);
              throw err;
            }
            return done(null, newUser, {message: 'Usuário cadastrado com sucesso'});
          });
        });
      };
      // Delay the execution of findOrCreateUser and execute the method
      // in the next tick of the event loop
      process.nextTick(findOrCreateUser);
    })
    );
};