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

查看:79
本文介绍了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 Function.
  • 将整个用户创建交给Cloud Functions,然后将email + password + displayName传递到您的自定义函数中,然后再创建用户帐户,设置其显示名称并为该用户创建文档.

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

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