Open
Conversation
…ed access_token and fb metadata.
Check user.fb.isNew to check if user was just created. Useful for email & other data verification.
Sometimes this would error out - bad connection, who knows what, and create an additional user object for you. Bad mongoose-auth.
Author
|
Using the above code simplifies the example code given for using Facebook and password login simultaneously. See the call to user.updateFBData(): findOrCreateUser: function (session, accessTok, accessTokExtra, fbUser) {
var promise = this.Promise()
, User = this.User()();
User.findById(session.auth.userId, function (err, user) {
if (err) return promise.fail(err);
if (!user) {
User.where('password.email', fbUser.email).findOne( function (err, user) {
if (err) return promise.fail(err);
if (!user) {
User.createWithFB(fbUser, accessTok, accessTokExtra.expires, function (err, createdUser) {
if (err) return promise.fail(err);
return promise.fulfill(createdUser);
});
} else {
user.updateFBData(fbUser, accessTok, accessTokExtra, function(err, user){
if (err) return promise.fail(err);
promise.fulfill(user);
});
}
});
} else {
user.updateFBData(fbUser, accessTok, accessTokExtra, function(err, user){
if (err) return promise.fail(err);
promise.fulfill(user);
});
}
});
return promise; // Make sure to return the promise that promises the user
} |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added update support for Facebook: subsequent logins will update stored access_token and fb metadata.
I noticed the FB data stored by mongoose-auth on first login quickly becomes stale. This addition to findOrCreateUser will update the stored data with new data when available.
I also added an 'isNew' field to the schema as well. This is true if and only if the user is completely fresh. We use this locally for asking the user to verify certain data, e.g. email address.