在验证现有身份验证用户的电子邮件后更新 Firestore 文档?

时间:2021-01-19 23:47:23

标签: firebase google-cloud-firestore firebase-authentication

我正在尝试让我的经过身份验证的用户(电子邮件/密码用户)能够更新他们的电子邮件地址。成功验证新电子邮件地址后,我需要更新用户的 Firestore 文档,该文档位于 users 集合中。

我正在这样做:

async updateEmail() {
      const authUser = this.$fire.auth.currentUser
      try {
        await authUser.updateEmail(this.email) // grabbing new email address from UI form
        await this.sendEmailVerification() // sending verification email to new address with action code

        // Here I will log out the user and send them to a new page stating to 
        // check their updated email account for an email verification message
       
        this.$fire.auth.signOut()
        this.$router.replace({name: 'email-confirmation-message')
        } catch () {
            console.error(error)
        }

问题:只有在验证新电子邮件地址后,我才需要使用以下属性更新用户的文档:

        await this.$fire.firestore
        .collection('users')
        .doc(authUser.uid)
        .update({
        email: this.email,
        emailVerified: true 
        })

在验证电子邮件之前,用户应该无法登录应用程序。用户的记录还应包含更新后的电子邮件地址。

此逻辑是否应放在 onAuthStateChanged 侦听器逻辑、云函数或 .. 中?任何我可以查看的野外示例?

(我还需要记住安全规则,因为我目前只有具有经过验证的电子邮件地址的用户,已登录,并将文档 uid 与 auth uid 匹配。但是,可以相应地调整它...)

>

谢谢!

1 个答案:

答案 0 :(得分:-1)

我会考虑通过 Cloud Function 而不是客户端 API 对用户文档进行这种类型的更改。这是一个有点敏感的操作(更改用户的身份),因此启用/允许更新用户文档我会有点偏执(潜在的安全漏洞)。

我有点惊讶没有 onUpdate 触发器 for Authentication。这对您的用例非常有用。而且我不确定是否有某种类型的触发器可以在用户验证其电子邮件地址时进行编码。