6

I try to implement .onCreate firebase cloud function like in Firebase cloud functions codeLab (https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#7)

exports.addWelcomeMessages = functions.auth.user().onCreate(event => {
  const user = event.data;
  console.log('user: %j', user);
  const fullName = user.displayName;

  return admin.database().ref('messages').push({
    name: 'Firebase Bot',
    text: `${fullName} signed in for the first time! Welcome!`
  });
});

In my client iOS application I use email authentication flow via FirebaseUI, that requires user to enter his email, name and password to sign up. After user signed up, .onCreate trigger in cloud functions fires, but user structure does not contain displayName data:

user: {"email":"my@gmail.com","metadata":{"createdAt":"2017-04-05T05:30:05.000Z","lastSignedInAt":"2017-04-05T05:30:05.000Z"},"uid":"wpIPNvf2suQdMumsEyXA5lQatt23"}

Notice: displayName property in FIRUser instance of current user in iOS app is filled correctly just after sign up process.

My assume that it is some kind of a bug in FirebaseUI that firstly creates an empty user with email, fires .onCreate trigger and only after this fills displayName property. Is it?

If not, what can be the cause of it?

1

2 Answers 2

10

This answer is not the proper method that the Firebase developers intended, but seeing as the code in the documentation does not work, since the users displayName property is not retrieved when an account is first created, I've come up with a simple workaround.

This code just uses the admin Node.js SDK found here: (https://firebase.google.com/docs/auth/admin/manage-users)

Here's the code:

const admin = require('firebase-admin'); // This is the required SDK
admin.initializeApp(functions.config().firebase); // SDK initialization is required 

exports.addWelcomeMessages = functions.auth.user().onCreate(event => {
  const user = event.data;
  console.log('user: %j', user);
  const uid = user.uid;

  return admin.auth().getUser(uid)
  .then(function(userRecord){
    // See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully fetched user data:", userRecord.toJSON());
    const fullName = userRecord.displayName || 'Anonymous';
    return admin.database().ref('messages').push({
      name: 'Firebase Bot',
      text: `${fullName} signed in for the first time! Welcome!`
    });
  })
  .catch(function(error){
    console.log("Error fetching user data:", error);
  });
});

This code has to make an additional call to the database, but at least it solves the issue of the displayName not being available in the onCreate event data. It works in my own testing, but hopefully someone is able to solve the actual issue without a workaround.

Let me know if this doesn't work for you! Cheers!

4
  • Is this another bug? Firebase is cool and stuff but with that many problems I faced with it lately, I am thinking of using only Firestore with NodeJs/PHP or maybe leave it completely and use MongoDB... Is it worth to continue?
    – Telion
    Feb 1, 2018 at 0:22
  • This works perfectly! As a suggestion, to avoid warnings, I changed the anonymous function calls to arrow callbacks. Instead of then(function(userRecord){...}) I used then((userRecord) => {...}) and the same on the catch function.
    – Albert
    Jun 13, 2018 at 14:16
  • thanks for sharing this. I'm having a lot of problems implementing what is shown in the Firebase documentation- for me, very little of it seems to work as shown in those docs (in react).
    – Mel
    Jan 22, 2020 at 0:21
  • @Mel there's a lot of things that could be better documented that firebase has, but this was one of the few workarounds I had to make like this. The majority of Firebase does work very well in my experience, so I don't think you'll have to worry about too many scenarios like this. Hope that helps your case. Jan 22, 2020 at 0:40
-1

For setting the user display name you first have to create the user.

It won't have the display name property at first.

After creating the user, you can then fire a modify request to add the display name to the user.

1
  • I'm sorry, but my question was not about manual user creation.
    – supp-f
    Apr 5, 2017 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.