The 'Passport' authentication module requires the FindOrCreate method to log in. I use mongoose to save my users with the following scheme:
var UserSchema = new Schema({
firstname: String,
lastname: String,
email: String,
accounts: []
});
An array of accounts stores objects that represent facebook accounts, for example {provider: "facebook", uid: "someFacebookId"}.
My authentication strategy looks like this:
passport.use(new FacebookStrategy({
clientID: CONFIG.fb.appId,
clientSecret: CONFIG.fb.appSecret,
callbackURL: CONFIG.fb.callbackURL
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function () {
User.find({ 'accounts.uid': profile.id, 'accounts.provider': 'facebook' }, function(err, olduser) {
if(olduser._id) {
console.log('User: ' + olduser.firstname + ' ' + olduser.lastname + ' found and logged in!');
done(null, olduser);
} else {
var newuser = new User();
var account = {provider: "facebook", uid: profile.id};
newuser.accounts.push(account);
newuser.firstname = profile.name.givenName;
newuser.lastname = profile.name.familyName;
newuser.email = "TBD...";
newuser.save(function(err) {
if(err) { throw err; }
console.log('New user: ' + newuser.firstname + ' ' + newuser.lastname + ' created and logged in!');
done(null, newuser);
});
}
});
});
}
));
Problem: After querying my database ( User.find(...)), the callback function is executed immediately without waiting for my database to respond. This results in an undefined object olduser. Therefore, I get a duplicate of the same user in my database every time that user tries to log in.
How to handle this asynchronous callback correctly?