【问题标题】:Firebase Cloud Function - null user.displayName onCreateFirebase 云函数 - null user.displayName onCreate
【发布时间】:2020-06-12 06:12:28
【问题描述】:

我正在尝试编写一个在创建用户时发送欢迎电子邮件的函数。我跟着this 教程,它说可以使用user.displayName 访问新创建的用户的displayName,尽管它一直为我返回null。我意识到发生这种情况的可能原因(如果我错了,请纠正我)是注册和设置用户的 displayName 发生在客户端的两个单独步骤中,因此当onCreate 被触发时,user.displayName 自然会是空值。这是我的客户端代码,供参考:

fb.auth().createUserWithEmailAndPassword(payload.email, payload.password).then(user => {
  return user.user.updateProfile({ displayName: payload.name, });
}).catch(/* ... */);

我正在寻找的是在user.updateProfile 上触发的云功能。我查看了auth.user().onOperation 函数(找到here),但是当我尝试将此函数部署到firebase 时,我收到错误Error: Functions did not deploy properly.(有帮助,ikr),我猜这与onOperation 函数是 private(如果我错了,请纠正我)。

有什么办法可以做我想做的事吗?如果是这样,怎么做?或者,有没有办法在createUserWithEmailAndPassword 上设置displayName,以便我可以继续使用onCreate 功能?

这是我当前的onCreate 代码:

exports.sendWelcomeEmail = functions.auth.user().onCreate(user => {
  console.log('name:', user.displayName);
});

这是我对onOperation 函数的尝试:

exports.sendWelcomeEmail = functions.auth.user().onOperation(user => {
  console.log('user:', user);
}, 'updateProfile');

【问题讨论】:

    标签: javascript firebase firebase-authentication google-cloud-functions


    【解决方案1】:

    如果我在你那里,我不会对用户的个人资料使用 firebase 身份验证。也许使用users 的集合是一个更好的主意,您可以在其中访问更新触发器。

    通常,我所做的是在有新的身份验证用户时触发,在userscollection 中创建一个用户。使用这种设计,您可以在每次有人更新其个人资料时触发。

    exports.onCreateAuthUser = functions.auth.user().onCreate(user => {
      firestore.collection('users').doc(user.uid).set({
         displayName: user.displayName,
         email: user.email,
         // any other properties 
      })
    
      //do other stuff
    });
    
    exports.onUpdateUser = functions
      .firestore.document('users/{id}')
      .onUpdate((change, context) => {
         // do stuff when user's profile gets updated
    }
    

    希望有帮助:)

    【讨论】:

      【解决方案2】:

      目前没有用于更新 Firebase 身份验证配置文件的 Cloud Functions 触发器。只有 onCreate 和 onDelete。

      见:Firebase auth onUpdate cloud function for when a user updates their email

      目前无法在使用电子邮件/密码身份验证创建帐户期间设置用户的 displayName 属性。创建帐户后,需要再次调用以更新配置文件。

      见:How do I set the displayName of Firebase user?

      基本上,您将不得不解决这些限制。请随时 contact Firebase support to file a feature request 以简化此操作。

      【讨论】:

        猜你喜欢
        • 2018-09-14
        • 1970-01-01
        • 2021-03-04
        • 2019-02-27
        • 2018-10-16
        • 1970-01-01
        • 2018-11-17
        • 2019-08-17
        • 2019-12-19
        相关资源
        最近更新 更多