【发布时间】:2021-04-24 07:47:21
【问题描述】:
我正在尝试让经过身份验证的用户(电子邮件/密码用户)能够更新他们的电子邮件地址。成功验证新的电子邮件地址后,我需要更新位于 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 监听器逻辑、云函数或..?有没有我可以看看的野外例子?
(我还需要牢记安全规则,因为我目前只有具有经过验证的电子邮件地址、已登录并将 doc uid 与 auth uid 匹配的用户。但是,可以相应地调整它...)
谢谢!
【问题讨论】:
-
我相信这应该放在您的更新电子邮件页面中,因为您无论如何都要退出您的用户,并且 onAuthStateChanged 会对此做出反应。如果您想使用 Cloud Functions,那么您应该删除旧用户并创建一个新用户,该用户拥有旧用户的所有数据。你可以利用onCreate and onDelete triggers with auth for Functions
-
所以我才意识到我没有真正的理由将用户的电子邮件地址存储在文档中,因为 Auth 已经包含该记录。我将只引用该属性的 Auth。为我解决这个问题!感谢您的帮助。
-
您能否将其发布为社区参考的答案?
标签: firebase google-cloud-firestore firebase-authentication