Created
December 4, 2014 16:59
-
-
Save tdd/b00822b246cf81b5ff48 to your computer and use it in GitHub Desktop.
findOrCreateByAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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