Email/Password auth provider returns empty userRecord

时间:2019-01-09 21:47:52

标签: firebase google-cloud-functions

I have a cloud function that is triggered by Auth user creation. I look up the user data (email, name, etc) to populate my DB. It suddenly stopped working for the 'email/password' Auth provider type. The admin.auth().getUser(uid) now returns a userRecord which contains undefined/null values for most fields. This seemingly stopped working out of nowhere in Production after functioning for several weeks, is there any possible explanation?

exports.createUser = functions.auth.user().onCreate((user) => {
    return createEmailUser(user);
});

function createEmailUser(user) {
    const uid = user.uid;
    return admin.auth().getUser(uid)
    .then(function(userRecord) {
        console.log(userRecord);
        const email = userRecord.email;
        const fullName = userRecord.displayName;
        admin.database().ref('users/' + uid).set({
            email: email,
            name: fullName
        });
    })
    .catch(function(error) {
        console.log("Error fetching user data:", error);
    });
}

In the past, the userRecord object contains valid email and displayName values. Now, I see an object like this:

UserRecord {
    uid: 'Lrtj8zafsnYjZl4ckMgwNkgEiVH2',
    email: undefined,
    emailVerified: false,
    displayName: undefined,
    photoURL: undefined,
    phoneNumber: undefined,
    disabled: false,
    metadata: 
        UserMetadata {
            creationTime: 'Wed, 09 Jan 2019 21:40:31 GMT',
            lastSignInTime: null },
    providerData: [],
    passwordHash: undefined,
    passwordSalt: undefined,
    customClaims: undefined,
    tokensValidAfterTime: 'Wed, 09 Jan 2019 21:40:31 GMT' }

1 个答案:

答案 0 :(得分:0)

由于使用电子邮件/密码方法注册了用户,因此userRecord中只有可用的电子邮件地址。其他登录提供商在创建用户时可能会有不同的数据。

您可以在此处创建个人资料时检查用户数据,并在缺少任何内容时使用updateUser更新个人资料:

function createEmailUser(user) {
    const uid = user.uid;

    admin.auth().updateUser(uid, {
      phoneNumber: "+11234567890",
      displayName: "Foo Bar"
    })

    .then(function(userRecord) {
          console.log(userRecord);
    })
    .catch(function(error) {
        console.log("Error fetching user data:", error);
    });
}