functions/index.js
/**
 * Creates a Firebase account with the given user profile and returns a custom auth token allowing
 * signing-in this account.
 * Also saves the accessToken to the datastore at /linkedInAccessToken/$uid
 *
 * @returns {Promise<string>} The Firebase custom auth token in a promise.
 */
Creates a Firebase account with the given user profile and returns a custom auth token allowing
signing-in this account.
Also saves the accessToken to the datastore at /linkedInAccessToken/$uid

@returns {Promise} The Firebase custom auth token in a promise.
Creates a Firebase account with the given user profile and returns a custom auth token allowing signing - in this account . Also saves the accessToken to the datastore at / linkedInAccessToken / $uid @returns { Promise } The Firebase custom auth token in a promise .
function createFirebaseAccount(uid, displayName, photoURL, email){
const userTokenTask = admin.auth().updateUser(uid, {
    displayName: displayName,
    photoURL: photoURL,
    email: email,
    emailVerified: true
  }).catch((error) => {
    if (error.code === 'auth/user-not-found') {
		console.log("Attempting to create a new account for: ", email);
		const createUserTask = admin.auth().createUser({
					uid: uid,
					displayName: displayName,
					photoURL: photoURL,
					email: email,
					emailVerified: true
				})
		  .catch(function(error) {
					console.log("Error in createUserTask: ", error);
			});	  
		const searchExistingUserTask = db.collection('private_data').where("email", "==", email).get()
			.then((querySnapshot) => {
				if(querySnapshot.docs.length > 0){
					console.log("Found existing user for ", email, " ==> ", querySnapshot.docs[0].id);
					member.uid_old = querySnapshot.docs[0].id;
					if(querySnapshot.docs[0].data().interests != undefined) private_data.interests = querySnapshot.docs[0].data().interests;
					if(querySnapshot.docs[0].data().comments != undefined) private_data.comments = querySnapshot.docs[0].data().comments;	
					return db.collection('members').doc(querySnapshot.docs[0].id).get();
				}
				console.log("Unable to find existing user for ", email);
				return false
			})
			.then((doc) => {
				if(doc && doc.exists){
					console.log("Found existing member record for ", email);
					if(doc.data().current_address != undefined) member.current_address = doc.data().current_address;
					if(doc.data().hometown_address != undefined) member.hometown_address = doc.data().hometown_address;
					if(doc.data().privacy != undefined) member.privacy = doc.data().privacy;
					member.date_updated = "-1"; 
					return db.collection('members').doc(doc.id).set({
						copied_account: true,
						uid_new: uid
						},
						{merge: true});
				}
				console.log("Didn't find existing member record for ", email);
				return true
			})
			.catch(function(error) {
					console.log("Error in searchExistingUserTask: ", error);
			});
		return Promise.all([createUserTask, searchExistingUserTask]).then(() => {
			return false;
		});
    } 
    throw error;
  })
  .then(() => {
		const token = admin.auth().createCustomToken(uid);
		console.log("About to write member database record: ", uid);
		private_data.email = email || "";
		console.log(private_data);
		console.log(member);
		const privateDatabaseTask = db.collection('private_data').doc(uid).set(
		private_data,
		{merge: true}
		)
		.catch(function(error){
		console.log(error);
		console.log("Error writing private database properties for ", uid);
		});
		const memberDatabaseTask = db.collection('members').doc(uid).set(
		member,
		{merge: true}
		)
		.catch(function(error){
		console.log(error);
		console.log("Error writing public database properties for ", uid);
		});
		return Promise.all([token, memberDatabaseTask, privateDatabaseTask]).then(() => {
			return token;
		}); 
  })
  return userTokenTask 
}
function createFirebaseAccount ( uid , displayName , photoURL , email ) { const userTokenTask = admin . auth ( ) . updateUser ( uid , { displayName : displayName , photoURL : photoURL , email : email , emailVerified : true } ) . catch ( ( error ) => { if ( error . code === 'auth/user-not-found' ) { console . log ( "Attempting to create a new account for: " , email ) ; const createUserTask = admin . auth ( ) . createUser ( { uid : uid , displayName : displayName , photoURL : photoURL , email : email , emailVerified : true } ) . catch ( function ( error ) { console . log ( "Error in createUserTask: " , error ) ; } ) ; const searchExistingUserTask = db . collection ( 'private_data' ) . where ( "email" , "==" , email ) . get ( ) . then ( ( querySnapshot ) => { if ( querySnapshot . docs . length > 0 ) { console . log ( "Found existing user for " , email , " ==> " , querySnapshot . docs [ 0 ] . id ) ; member . uid_old = querySnapshot . docs [ 0 ] . id ; if ( querySnapshot . docs [ 0 ] . data ( ) . interests != undefined ) private_data . interests = querySnapshot . docs [ 0 ] . data ( ) . interests ; if ( querySnapshot . docs [ 0 ] . data ( ) . comments != undefined ) private_data . comments = querySnapshot . docs [ 0 ] . data ( ) . comments ; return db . collection ( 'members' ) . doc ( querySnapshot . docs [ 0 ] . id ) . get ( ) ; } console . log ( "Unable to find existing user for " , email ) ; return false } ) . then ( ( doc ) => { if ( doc && doc . exists ) { console . log ( "Found existing member record for " , email ) ; if ( doc . data ( ) . current_address != undefined ) member . current_address = doc . data ( ) . current_address ; if ( doc . data ( ) . hometown_address != undefined ) member . hometown_address = doc . data ( ) . hometown_address ; if ( doc . data ( ) . privacy != undefined ) member . privacy = doc . data ( ) . privacy ; member . date_updated = "-1" ; return db . collection ( 'members' ) . doc ( doc . id ) . set ( { copied_account : true , uid_new : uid } , { merge : true } ) ; } console . log ( "Didn't find existing member record for " , email ) ; return true } ) . catch ( function ( error ) { console . log ( "Error in searchExistingUserTask: " , error ) ; } ) ; return Promise . all ( [ createUserTask , searchExistingUserTask ] ) . then ( ( ) => { return false ; } ) ; } throw error ; } ) . then ( ( ) => { const token = admin . auth ( ) . createCustomToken ( uid ) ; console . log ( "About to write member database record: " , uid ) ; private_data . email = email || "" ; console . log ( private_data ) ; console . log ( member ) ; const privateDatabaseTask = db . collection ( 'private_data' ) . doc ( uid ) . set ( private_data , { merge : true } ) . catch ( function ( error ) { console . log ( error ) ; console . log ( "Error writing private database properties for " , uid ) ; } ) ; const memberDatabaseTask = db . collection ( 'members' ) . doc ( uid ) . set ( member , { merge : true } ) . catch ( function ( error ) { console . log ( error ) ; console . log ( "Error writing public database properties for " , uid ) ; } ) ; return Promise . all ( [ token , memberDatabaseTask , privateDatabaseTask ] ) . then ( ( ) => { return token ; } ) ; } ) return userTokenTask }
Creates a Firebase account with the given user profile and returns a custom auth token allowing
 signing-in this account.
Creates a Firebase account with the given user profile and returns a custom auth token allowing signing - in this account .
// Save the access token to the Firebase Realtime Database. // Taking out now, if add back replace in Promises at end //const databaseTask = admin.database().ref(`/linkedInAccessToken/${uid}`).set(accessToken); // Create or update the user account. // If user does not exists we create it. //var uidOld; // Create user account // See if user by same email exists in database. If so // a) Save their interests/comments from retrieved private_data record // b) Search for members record and save addresses // c) Set flags on old members record  //Set to -1 to trigger profile form update	 // END IF // END Catch // Create a Firebase custom auth token. //holds value of token 