我正在尝试让我的经过身份验证的用户(电子邮件/密码用户)能够更新他们的电子邮件地址。成功验证新电子邮件地址后,我需要更新位于
users
集合中的用户的 firestore 文档。
我正在做这个:
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 与身份验证 uid 相匹配。但是,可以相应地调整它...)
谢谢!
讨论有点晚了,但正如 @renaud-tarnec 所写,到目前为止,Firebase 不支持身份验证的 onUpdate 触发器。我实施的解决方法是在用户每次登录网站时检查电子邮件是否与身份验证和 firestore 文档匹配。此外,当用户更改他们的电子邮件/密码时,我会注销他们以强制他们登录。希望它有帮助!
我会考虑通过云函数而不是客户端 API 对用户文档进行此类更改。 这是一个有点敏感的操作(更改用户的身份),因此启用/允许更新用户文档我会有点偏执(潜在的安全漏洞)。
我有点惊讶没有
onUpdate
触发器用于身份验证。 这对您的用例来说非常有用。 我不确定当用户验证他们的电子邮件地址时是否可以编写某种类型的触发器。