1
votes

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

1 Answers

0
votes

As users are registered with Email/Password method, then there is only the email address available in userRecord. Other sign-in providers might have different data at user creation.

What you can do here is to check user data at profile creation, and update profile with updateUser if anything is missing:

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