1

I am trying to create a cloud function with firebase functions to detect if the user emailVerified prop changes. This is what I currently have in my index.js in functions folder:

exports.emailVerifiedChange = functions.auth.user().onCreate((user)=> {
    if(user.emailVerified) {
        admin.firestore().collection(`users`).doc(user.uid).create({
            created: new Date(),
            uid: user.uid,
            name: user.displayName,
            searchName: user.displayName.toLowerCase(),
            userInfo: {
              profilePic: '',
              phoneNumber: '',
              email: user.email
            },
            provider: false 
        })
    }
}) 

In the log, this function is called when a user is created but the user collection is not created. Any idea? I'm new to functions so not sure why its not working.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
User123123
  • 485
  • 1
  • 5
  • 13

2 Answers2

1

I am trying to create a cloud function with firebase functions to detect if the user emailVerified prop changes.

This isn't possible in Cloud Functions, as there's no trigger when the user object changes. The onCreate trigger you use (as its name implies) only triggers when the user is first created.

Having a trigger when the user profile changes would be a useful addition to Cloud Functions, so I recommend you file feature request for it.

The only workaround I can think of for now is to detect the emailVerified status client-side, call a callable or HTTPS Cloud Function with the ID token, verify that ID token, and then store the emailVerified value to Firestore from there.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • the reason im trying to do this is to avoid creating collections if a person is creating fake accounts etc. want to avoid creating user documents. is there a specifc way I can do this? Cuz if the user closes the website, and then verifies their email. Then I am unable to create their collection. – User123123 Dec 13 '22 at 16:24
  • Or is there a way of sending the user to a custom page on my website when they verify their email so I can handle the logic there? – User123123 Dec 13 '22 at 16:29
  • You'll need a trusted environment to verify their email address. I suggested Cloud Functions in my last paragraph, but that can also run another trusted environment. You can also let the clients create those collections themselves, and then [check whether their email is verified in the security rules of your database](https://stackoverflow.com/a/50239804/209103). – Frank van Puffelen Dec 13 '22 at 17:42
0

The API reference documentation shows:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  }
});
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91