SeriousLee
SeriousLee

Reputation: 1371

Firebase Cloud Function - null user.displayName onCreate

I'm trying to write a function that will send a welcome email on user creation. I followed along to this tutorial, which says that one can access the newly created user's displayName with user.displayName, though it keeps returning null for me. I realized that the probable reason that this happens (correct me if I'm wrong here) is that registration and setting the user's displayName happens in two separate steps client-side and therefore when onCreate is triggered, user.displayName will naturally be null. Here's my client-side code, for reference:

fb.auth().createUserWithEmailAndPassword(payload.email, payload.password).then(user => {
  return user.user.updateProfile({ displayName: payload.name, });
}).catch(/* ... */);

What I'm looking for is a cloud function that gets triggered on user.updateProfile. I've looked into the auth.user().onOperation function (found here), but when I try to deploy this function to firebase, I get the error Error: Functions did not deploy properly. (helpful, ikr), which I guess has something to do with the onOperation function being private (correct me if I'm wrong).

Is there any way to do what I'm trying to do? If so, how? Alternatively, is there a way to set the displayName on createUserWithEmailAndPassword, so that I can keep using the onCreate function?

Here's my current onCreate code:

exports.sendWelcomeEmail = functions.auth.user().onCreate(user => {
  console.log('name:', user.displayName);
});

And here's my attempt at the onOperation function:

exports.sendWelcomeEmail = functions.auth.user().onOperation(user => {
  console.log('user:', user);
}, 'updateProfile');

Upvotes: 6

Views: 1519

Answers (3)

martsie
martsie

Reputation: 1

Unfortunately Firebase is limited in that it doesn't allow you to save display names on user create, and updating profile doesn't have any triggers.

If you still want to use the user create trigger but have access to the display name a work around is to re-query the user until they have a display name.

It'll increase your function execution time by a second or two but if it's just signups then it might not be very expensive in the grand scheme of things.

export const myUserCreateFunction = functions
  .auth.user()
  .onCreate(async (user) => {
    let newUser = user;
      let retries = 0;
      while (!newUser.displayName && retries < 10) {
        await new Promise((resolve) => setTimeout(resolve, 1000));
        newUser = await admin.auth().getUser(user.uid);
        retries++;
      }

      console.log(newUser.displayName, "users display name here!");

      if (!newUser.displayName) {
        throw new HttpsError("failed-precondition", "User name not found");
      }
}

Upvotes: 0

ajorquera
ajorquera

Reputation: 1309

If I where you, I wouldnt use firebase auth for user's profile. Maybe is a better idea to use a collection of users, where you have access to update triggers.

Normally, what I do is to have a trigger when there is new auth user, create a user in the userscollection. With this design you could have a trigger everytime someone updates it's profile.

exports.onCreateAuthUser = functions.auth.user().onCreate(user => {
  firestore.collection('users').doc(user.uid).set({
     displayName: user.displayName,
     email: user.email,
     // any other properties 
  })

  //do other stuff
});

exports.onUpdateUser = functions
  .firestore.document('users/{id}')
  .onUpdate((change, context) => {
     // do stuff when user's profile gets updated
}

Hope that helps :)

Upvotes: 5

Doug Stevenson
Doug Stevenson

Reputation: 317712

There is currently no Cloud Functions trigger for updating a Firebase Auth profile. There is just onCreate and onDelete.

See: Firebase auth onUpdate cloud function for when a user updates their email

There is currently no way to set a user's displayName property during the creation of an account using email/password authentication. It requires a second call to update the profile after the account is created.

See: How do I set the displayName of Firebase user?

Basically, you are going to have to work around these limitations. Feel free to contact Firebase support to file a feature request to make this easier.

Upvotes: 7

Related Questions