【问题标题】:How to change display name in firebase cloud functions?如何更改 Firebase 云功能中的显示名称?
【发布时间】:2020-06-10 16:23:24
【问题描述】:

我正在尝试构建一个 firebase 云函数来更改显示名称。

我知道我可以在 android 中轻松做到这一点,但是因为每次我更改显示名称时我都需要更新所有用户记录,而且似乎没有办法在显示名称时在 firebase 函数中获得通知已更改(如果我错了请纠正我),我计划在 firebase 中进行更改,同时更新记录。

我就是这样开始的......

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

});

【问题讨论】:

  • 你有什么问题?听起来你只需要完成你的功能。

标签: node.js firebase-authentication google-cloud-functions


【解决方案1】:

要从 Cloud Functions 更新用户名,您将使用适用于 Node.js 用户的 Firebase Admin SDK。通过这个,你可以update any properties of a user profile 使用这样的东西:

admin.auth().updateUser(uid, {
  email: 'modifiedUser@example.com',
  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);
  });

【讨论】:

  • 函数在没有任何警告或错误的情况下通过,但是当我在 android 中读取 FirebaseAuth.getInstance().getCurrentUser().getDisplayName() 时,并没有改变,这有意义吗?我需要刷新本地身份验证吗?
  • 找到了。 FirebaseAuth.getInstance().getCurrentUser().reload();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 2019-01-15
  • 1970-01-01
  • 2020-01-24
  • 2019-02-13
  • 1970-01-01
  • 2017-04-10
相关资源
最近更新 更多