0
votes

Strange issue, I have a function that trigger by Firebase newUser auth if the user uses Google or Facebook provider the function works but if it email/password signup the function returns an undefined error. what I'm missing here?

The function code:

  exports.newUser = functions.auth.user().onCreate((user) => {
    const docRef = admin.firestore().collection('Users').doc(user.uid)

    return docRef.set({
        email: user.email,
        name: '',
        photo: '',
        signupDate: admin.firestore.FieldValue.serverTimestamp()
      },{merge: true});
  });
2
ok it seems that the issue is in this field email: user.email if user signup with email and this newUser function been triggered, how email field is undefined. - Michael Vish
why on Facebook & Google providers this field defined but on email/password signup it's undefined? - Michael Vish

2 Answers

1
votes

The documentation for the email property of UserRecord suggests that it is not always available. So, you should check for that in your code.

0
votes

user.email field appears under provider and not like in the Firebase Example directly under user.

here how it should look like:

var email = user.email; // The email of the user.
if (email == undefined) {
  for (var provider of user.providerData) {
    if (provider.email) {
      email = provider.email;
      break;
    }
  }
}

 return docRef.set({
      email: email,
      name: '',
      photo: '',
      signupDate: admin.firestore.FieldValue.serverTimestamp()
    },{merge: true});
});