Michael Vish
Michael Vish

Reputation: 153

Undefined error on simple firebase auth function

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});
  });

Upvotes: 0

Views: 851

Answers (2)

Michael Vish
Michael Vish

Reputation: 153

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});
});

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317372

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

Upvotes: 1

Related Questions