epic
epic

Reputation: 1443

How to change display name in firebase cloud functions?

I am trying to build a firebase cloud function to change a display name.

I know that i can do it easily in android, but since every time i change the display name i need to update all user records and since there isn't seem to be a way to get notified in firebase function when display name has changed (please correct me if i am wrong), i am planning to make the change in firebase and on the same time make the records update.

This is how i started...

exports.changeDisplayName = functions.https.onCall((data,context) => {
console.log(`User ${context.auth.uid} has requested to change its display name`);

//If user not authenitacted, throw an error
if (!(context.auth && context.auth.token && (context.auth.token.firebase.sign_in_provider === 'phone'))) {
    
    if (!context.auth) {
        console.log(`User ${context.auth.uid} without auth`);
    }

    if (!context.auth.token) {
        console.log(`User ${context.auth.uid} without token`);
    }

    if (!context.auth.token.phoneNumber) {
        console.log(`User ${context.auth.uid} without phone number (${context.auth.token.firebase.sign_in_provider})`);
    }

    throw new functions.https.HttpsError(
        'permission-denied',
        'Must be an authenticated user with cellphone to change display name'
      );
}

// Change display display name from data

// update firebase contacts

});

Upvotes: 2

Views: 2256

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599601

To update a user's name from Cloud Functions, you'll be using the Firebase Admin SDK for Node.js users. Through that, you can update any properties of a user profile with something like this:

admin.auth().updateUser(uid, {
  email: '[email protected]',
  phoneNumber: '+11234567890',
  emailVerified: true,
  password: 'newPassword',
  displayName: 'Jane Doe',
  photoURL: 'http://www.example.com/12345678/photo.png',
  disabled: true
})
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log('Successfully updated user', userRecord.toJSON());
  })
  .catch(function(error) {
    console.log('Error updating user:', error);
  });

Upvotes: 3

Related Questions