User displayName is undefined in .onCreate trigger for email sign-up method

蹲街弑〆低调 提交于 2019-12-12 16:20:46

问题


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:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/43223896/user-displayname-is-undefined-in-oncreate-trigger-for-email-sign-up-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!