Guest User

Untitled

a guest
Jan 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. exports.findOrCreateFacebookUser = function(fbUserData, promise){
  2.  
  3. User.findOne({_id:fbUserData.id}, function(err, user) {
  4. if(err) {
  5. console.log("Error in finding user for auth. Check Db");
  6. promise.fail(err);
  7. return;
  8. }
  9. else if(user){
  10. console.log("User found ");
  11. promise.fulfill(user);
  12. }
  13. else{
  14.  
  15. var joiningUser = new User();
  16. joiningUser._id = fbUserData.id;
  17. joiningUser.firstName = fbUserData.first_name;
  18. joiningUser.lastName = fbUserData.last_name;
  19. joiningUser.email = fbUserData.email;
  20. //console.log(JSON.stringify(joiningUser));
  21. joiningUser.save(function(err){
  22. if(err){
  23. console.log("Couldnt save new user: " + err);
  24. promise.fail(err);
  25. return;
  26. }
  27. else{
  28. console.log("User wasnt existant, it is now created: " + JSON.stringify(joiningUser));
  29. promise.fulfill(joiningUser);
  30. }
  31. });
  32. }
  33. });
  34. };
Add Comment
Please, Sign In to add comment