Firebase auth.user API的云功能具有空的displayName属性 [英] Cloud Functions for Firebase auth.user API has empty displayName property

查看:55
本文介绍了Firebase auth.user API的云功能具有空的displayName属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Firebase并带有数据库,身份验证和云功能模块的Android应用.当用户通过应用创建个人资料并设置用户全名时,

I have an Android app using Firebase with database, authentication and cloud functions modules. When a user creates a profile through the app and sets a user full name, the value of

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // user signed in
    String displayName = user.getDisplayName();
    // do whatever with displayName...
} else {
    // ....
}

几乎与预期的一样-无论用户输入了什么.

is pretty much as expected - whatever the user put in.

我还具有云功能,该功能可以为新用户创建数据库记录:

I also have a cloud function, which creates a database record for the new user:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.createUserRecord = functions.auth.user().onCreate(function(event) {
    const user = event.data;
    const userName = user.displayName || 'Anonymous';
    const userSignedUp = (new Date()).toJSON();
    console.log('A new user signed up: ', user.uid, userName, userSignedUp);
    return admin.database().ref('users').push({
        name: `${userName}`,
        email: `${user.email}`,
        ref: `${user.uid}`,
        signup: `${userSignedUp}`
    });
});

除了日志条目和数据库列表中的结果记录 Anonymous 作为名称值之外,这几乎都可以,

This works mostly OK, except that both the log entry, and the resulting record in the database list Anonymous as the name value.

有人可以告诉我我要怎么做吗?谢谢.

Can anyone please tell me where I am going wrong with this? Thanks.

最诚挚的问候,

〜托多尔

更新:

忘记了,该用户是使用电子邮件/密码提供程序创建的.

Forgot to mention, the user is created with the email/password provider.

推荐答案

在FirebaseUI中,通过电子邮件/密码创建用户的过程分为两个步骤:使用地址和密码创建用户,然后更新用户配置文件输入的用户名.可以在FirebaseUI

In FirebaseUI, creation of a user via email/password is done in two steps: the user is created using the address and password, then the user profile is update with the entered username. This can be seen in the FirebaseUI RegisterEmailFragment.

目前,Cloud Functions仅提供触发用户创建,您正在使用的 functions.auth.user().onCreate().没有触发用户配置文件更新的触发器.创建用户时触发您的触发器.事件数据包含当时已存在的用户配置文件,而该用户配置文件已用用户名更新.

At this time, Cloud Functions offers only a trigger for user creation, the one you are using, functions.auth.user().onCreate(). There is no trigger for user profile update. Your trigger fires when the user is created. The event data contains the user profile as it exists at that moment, before it has been updated with the user name.

您将需要使用其他方式来获取用户名.一种选择是使用数据库事件来触发一个函数,该函数将检索用户个人资料更新后的数据:

You will need to use some other means to obtain the user name. One option is to use a database event to trigger a function that would retrieve the user data after the profile is updated:

admin.auth().getUser(uid)
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully fetched user data:", userRecord.toJSON());
  })
  .catch(function(error) {
    console.log("Error fetching user data:", error);
  });

这篇关于Firebase auth.user API的云功能具有空的displayName属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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