firebase 云功能认证:onCreate 事件不包含 displayName [英] firebase cloud function authentication: onCreate event doesn't contain displayName

查看:27
本文介绍了firebase 云功能认证:onCreate 事件不包含 displayName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 onCreate 的云函数,看起来像

I have a cloud function for onCreate that looks like

exports.addNewUserToCollection = functions.auth.user().onCreate(event => {
    const user = event.data; // The Firebase user.

    var userData = JSON.parse(JSON.stringify(user));
    if (!userData.displayName){
        userData.displayName = '(no name)'
    }

    return db
        .collection("users")
        .doc(user.uid)
        .set(userData);
});

除非用户通过电子邮件注册,否则它工作正常.
当用户通过电子邮件注册时,系统会提示他们输入名字和姓氏,并且此信息会包含在身份验证数据中.
我知道这一点是因为在他们的会话中我可以调用 getCurrentUser() 并检索他们的 displayName 属性.

it works fine except when the user signs up via email.
When the user signs up via email they are prompted for a first and last name and this information makes it into the authentication data.
I know this because inside their session I can call getCurrentUser() and retrieve their displayName property.

然而,上面代码中的事件数据不包含 displayName(或名字和姓氏)

The event data in the code above however does not contain displayName (or first and last name for that matter)

什么给了?

推荐答案

在 Firebase 身份验证中创建电子邮件+密码帐户时,您只需指定最少的信息:电子邮件和密码.例如在 iOS 上是:

When you create an email+password account in Firebase Authentication, you specify only the minimum information: email and password. For example on iOS this is:

Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
  // ...
}

您的云功能会通过以下精确信息触发:只有电子邮件和密码.

Your Cloud Function is triggered with this precise information: just the email and password.

虽然以后可以更新用户配置文件以包含显示名称,但该信息不会传递到 Cloud Function 触发器(目前仅在创建帐户时触发).

While it is possible to later update the user profile to include the display name, that information is not passed on to the Cloud Function trigger (which currently only triggers on account creation).

可能的解决方法是:

  • 当您更新用户配置文件以设置显示名称时,从您的代码调用云函数.
  • 将整个用户创建过程交给 Cloud Functions,将电子邮件+密码+显示名称传递给您的自定义函数,然后创建用户帐户、设置其显示名称并为该用户创建文档.

这篇关于firebase 云功能认证:onCreate 事件不包含 displayName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆