Home > Back-end >  Create a firebase cloud function that detects emailVerified change
Create a firebase cloud function that detects emailVerified change

Time:12-13

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.

CodePudding user response:

The API reference documentation shows:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  }
});

CodePudding user response:

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.

  • Related