0
votes

I have created a function that sets the user ID in the firestore database based on the email adress in the authentication tab of firebase. However, I need it to be a cloud function that triggers upon the first login. I have found documentation that mentioned this functionality, but I can't figure out how I have it trigger only on the first login, not when the user logs in for the second time and also not upon user creation. I have also provided the code below, perhaps it gives a better idea of what I need. Some lines are commented out, so I could test the rest of the code. I have found multiple threads about this topic, but I cant figure out how to exactly manage this.

https://firebase.google.com/docs/functions/auth-events#trigger_a_function_on_user_creation

//Detect first login from user
//if(firebase.auth.UserCredential.isNewUser()){
  if(true){
    //User is logged in for the first time
    //const userID = firebase.auth().currentUser.UID;
    //const userEmail = firebase.auth().currentUser.email;
    const userID = '1234567890';
    const userEmail = '[email protected]';
    var docFound = false;
    //Get email, either personal or work
    console.log('Taking a snapshot...');
    //Test for work email
    const snapshot = db.collectionGroup('people').where('email.work', '==', userEmail).get()
      .then(function(querySnapshot){
        querySnapshot.forEach(function(doc){
          //work email found
          console.log('work email found');
          console.log(doc.data()); 
          docFound = true;
          const organisationID = doc.ref.parent.parent.id;
          writeUID(doc.id, userID, organisationID);  
        });
      });
  
    if(!docFound){
      //Test for personal email
      const snapshot = db.collectionGroup('people').where('email.personal', '==', userEmail).get()
      .then(function(querySnapshot){
        querySnapshot.forEach(function(doc){
          //personal email found
          console.log('personal email found');
          console.log(doc.data()); 
          const organisationID = doc.ref.parent.parent.id;
          writeUID(doc.id, userID, organisationID);  
        });
      });
    }
  }
  async function writeUID(doc, uid, organisationID){ 
    const res = db.collection(`organisations/${organisationID}/people`).doc(doc).set({
      userId: uid
    }, { merge: true });  
  }  
  /*
  TODO: Detect first login
  TODO: Get correct user values
  Rest of the function works
  */

Thanks in advance for your help.

1
When a new user registers you need to create a node in Firebase Realtime Database something like hasLoggedInOnce: false or whatever the name could be! Whenever a user logs in to the application check for this value, if it's false it is first login and then change the value to true so next time it wont trigger stuff that are meant to be for first login only!Dharmaraj
As explained in my answer to one of your previous question, it is not possible to "natively" trigger a Cloud Function when a user logs in to your frontend application, as there is no such trigger among the Firebase Authentication triggers. You may build a mechanism like the one advised by @Dharmaraj which consists in triggering the CF based on a DB record you write after the login, from your app.Renaud Tarnec

1 Answers

1
votes

I can't figure out how I have it trigger only on the first login, not when the user logs in for the second time and also not upon user creation

What you're trying to do is not possible with Cloud Functions auth triggers.

Auth triggers only work when a user account is created or deleted. They don't trigger when a user signs in. Only your app knows when a user signs in or out - neither Firebase Auth nor Cloud Functions understands your specific definition of what a "first sign in" actually means. What you will have to do is detect your "first sign in" in your app code, then possibly call a function (HTTP or callable) to do some work on behalf of the user.