{"size": 835, "ext": "js", "max_stars_repo_path": "models/oauth_authcode.js", "max_stars_repo_name": "gaboelnuevo/node-auth-socket.io-example", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "models/oauth_authcode.js", "max_issues_repo_name": "gaboelnuevo/node-auth-socket.io-example", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "models/oauth_authcode.js", "max_forks_repo_name": "gaboelnuevo/node-auth-socket.io-example", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.5588235294, "max_line_length": 89, "alphanum_fraction": 0.6910179641}
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var OAuthAuthCodeSchema = new Schema({
  authCode: { type: String, required: true, unique: true },
  clientId: String,
  userId: { type: String, required: true },
  expires: Date
});

mongoose.model('oauth_authcodes', OAuthAuthCodeSchema);

var OAuthAuthCodeModel = mongoose.model('oauth_authcodes');

module.exports.getAuthCode = function(authCode, callback) {
  OAuthAuthCodeModel.findOne({ authCode: authCode }, callback);
};

module.exports.saveAuthCode = function(code, clientId, expires, userId, callback) {
  var fields = {
    clientId: clientId,
    userId: userId,
    expires: expires
  };

  OAuthAuthCodeModel.update({ authCode: code }, fields, { upsert: true }, function(err) {
    if (err) {
      console.error(err);
    }

    callback(err);
  });
};
