node.js - 如何更改firebase云函数中的显示名称?

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

我正在尝试构建一个 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

});

最佳答案

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

admin.auth().updateUser(uid, {
  email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="731e1c171a151a16172600160133160b121e031f165d101c1e" rel="noreferrer noopener nofollow">[email protected]</a>',
  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);
  });

关于node.js - 如何更改firebase云函数中的显示名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60419960/

相关文章:

javascript - Firebase promise 问题 - "Maximum call stack size exceeded"

javascript - 尝试使用 axios 从 Reactjs 组件发送数据到 Express 应用程序

javascript - 从两个不同的值创建一个公共(public)键,不考虑顺序

node.js - 如何使用 Node/Passport 获取用户的 LDAP OU?

javascript - 为什么这些调用按它们发生的顺序发生?

python - Google Cloud Functions (Python) 中的随机连接错误

javascript - 有没有办法在重定向到另一个页面后保持登录状态?

swift - 如何处理使用同一帐户登录的其他设备所做的更改?

firebase - 如何使用firebase在网络上嵌入谷歌登录按钮进行身份验证

go - 尝试使用 go 模块在 Go 1.11 中部署 Google 云功能时出错