Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
findOrCreateByAuth
userSchema.statics.findOrCreateByAuth = function findOrCreateByAuth(id, name, provider, done) {
var User = this;
User.update(
{ _id: id, provider: provider },
{ name: name, provider: provider },
{ upsert: true },
function(err, numAffected, details) {
if (err) {
return done(err);
}
if (details.updatedExisting) {
return done(null, id);
}
User.update(
{ _id: id, provider: provider },
{ joinedAt: Date.now() },
null,
function(err) { done(err, id); }
);
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment