<commit_before>var uuid = require('node-uuid');

var userSchema = require('./../schemata/user');
userSchema.method('login', function(data, providers, callback) {
  var user = this;
  var authorizer = providers[data.provider].authorizer;
  authorizer(data.uid, data.pw)(function (err, res) {
    if (err || !res) {
      callback(new Error("Login Failed"));
    } else {
      user.session = {
        provider: data.provider,
        uid: data.uid
      }
      user.token = uuid.v4();
      user.save(function () {
        callback(null)
      });
    }
  })
});

module.exports = require('mongoose').model('User', userSchema);
<commit_msg>find or create by authorizations on mongoose model<commit_after>var uuid = require('node-uuid');

var userSchema = require('./../schemata/user');
/* 
 * A user will login with local credentials (provider=local)
 * or with 3rd party credentials (provider=github) in either case
 * we want to findOrCreate a User and grant a fresh token. */
userSchema.statics.findOrCreateByAuthorization = function(data, providers, callback) {
  var authorizer = providers[data.provider].authorizer;
  authorizer(data.uid, data.pw)(function (err, providerData) {
    if (err) {
      callback(new Error("Login Failed"));
    } else {
      if (data.provider === 'local') {
        var user = providerData.user;
        user.token = uuid.v4();
        user.save(function () {
          callback(null, user)
        });
      } else {
        this.findOne()
        .where('authorizations.'+data.provider+'.login')
        .equals(providerData.login)
        .exec(function (err, user) {
          if (err) { return callback(err); }
          else if (user) {
            // Found the user
            if (! user.authorizations) user.authorizations = {};
            user.authorizations[data.provider] = providerData;
            user.token = uuid.v4();
            user.save(function () {
              callback(null, user)
            });
          } else {
            // Create the user
            var user = new User();
            if (! user.authorizations) user.authorizations = {};
            user.authorizations[data.provider] = providerData;
            user.token = uuid.v4();
            user.save(function () {
              callback(null, user)
            });
          }
        })
      }
    }
  }.bind(this))
};

var User = require('mongoose').model('User', userSchema);

module.exports = User;

{'fraction_non_alphanumeric': 0.14279852277390234, 'fraction_numerical': 0.002051702913418137, 'mean_word_length': 2.2768817204301075, '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': '7455321', 'n_tokens_mistral': 691, 'n_tokens_neox': 693, 'n_words': 160}