app/controllers/users/users.authentication.server.controller.js
/**
 * Helper function to save or update a OAuth user profile
 */
Helper function to save or update a OAuth user profile
Helper function to save or update a OAuth user profile
function createLoginFacebookUser (req, providerUserProfile, callback) {
	if (!req.user) {
        console.log('user',providerUserProfile);  
		var searchMainProviderIdentifierField = 'providerData.' + providerUserProfile.providerIdentifierField;
		var searchAdditionalProviderIdentifierField = 'additionalProvidersData.' + providerUserProfile.provider + '.' + providerUserProfile.providerIdentifierField;
		var mainProviderSearchQuery = {};
		mainProviderSearchQuery.provider = providerUserProfile.provider;
		mainProviderSearchQuery[searchMainProviderIdentifierField] = providerUserProfile.providerData[providerUserProfile.providerIdentifierField];
		var additionalProviderSearchQuery = {};
		additionalProviderSearchQuery[searchAdditionalProviderIdentifierField] = providerUserProfile.providerData[providerUserProfile.providerIdentifierField];
        console.log('Main Search Provider', mainProviderSearchQuery);
        console.log('additional Search Provider', additionalProviderSearchQuery);
		var searchQuery = {
			$or: [mainProviderSearchQuery, additionalProviderSearchQuery]
		};
		User.findOne(searchQuery, function(err, user) {
			if (err) {
				return callback(err);
			} else {
				if (!user) {
					var possibleUsername = providerUserProfile.username || ((providerUserProfile.email) ? providerUserProfile.email.split('@')[0] : '');
					User.findUniqueUsername(possibleUsername, null, function(availableUsername) {
						user = new User({
							firstName: providerUserProfile.firstName,
							lastName: providerUserProfile.lastName,
							username: availableUsername,
							displayName: providerUserProfile.displayName,
							email: providerUserProfile.email,
							provider: providerUserProfile.provider,
                            picture: providerUserProfile.picture,
                            picture_small: providerUserProfile.picture,
							providerData: providerUserProfile.providerData,
                            selectedBand: undefined
						});
						user.save(function(err, user) {
                            return callback(err, user);    
						});
					});
				} else {
					return callback(err, user);
				}
			}
		});
	}
}
function createLoginFacebookUser ( req , providerUserProfile , callback ) { if ( ! req . user ) { console . log ( 'user' , providerUserProfile ) ; var searchMainProviderIdentifierField = 'providerData.' + providerUserProfile . providerIdentifierField ; var searchAdditionalProviderIdentifierField = 'additionalProvidersData.' + providerUserProfile . provider + '.' + providerUserProfile . providerIdentifierField ; var mainProviderSearchQuery = { } ; mainProviderSearchQuery . provider = providerUserProfile . provider ; mainProviderSearchQuery [ searchMainProviderIdentifierField ] = providerUserProfile . providerData [ providerUserProfile . providerIdentifierField ] ; var additionalProviderSearchQuery = { } ; additionalProviderSearchQuery [ searchAdditionalProviderIdentifierField ] = providerUserProfile . providerData [ providerUserProfile . providerIdentifierField ] ; console . log ( 'Main Search Provider' , mainProviderSearchQuery ) ; console . log ( 'additional Search Provider' , additionalProviderSearchQuery ) ; var searchQuery = { $or : [ mainProviderSearchQuery , additionalProviderSearchQuery ] } ; User . findOne ( searchQuery , function ( err , user ) { if ( err ) { return callback ( err ) ; } else { if ( ! user ) { var possibleUsername = providerUserProfile . username || ( ( providerUserProfile . email ) ? providerUserProfile . email . split ( '@' ) [ 0 ] : '' ) ; User . findUniqueUsername ( possibleUsername , null , function ( availableUsername ) { user = new User ( { firstName : providerUserProfile . firstName , lastName : providerUserProfile . lastName , username : availableUsername , displayName : providerUserProfile . displayName , email : providerUserProfile . email , provider : providerUserProfile . provider , picture : providerUserProfile . picture , picture_small : providerUserProfile . picture , providerData : providerUserProfile . providerData , selectedBand : undefined } ) ; user . save ( function ( err , user ) { return callback ( err , user ) ; } ) ; } ) ; } else { return callback ( err , user ) ; } } } ) ; } }
Helper function to save or update a OAuth user profile
Helper function to save or update a OAuth user profile
// Define a search query fields // Define main provider search query // Define additional provider search query // Define a search query to find existing user with current provider profile // And save the user