【问题标题】:Firebase Auth: can't delete Firestore documents in promise returned from currentUser.delete()Firebase Auth:无法删除从 currentUser.delete() 返回的 Promise 中的 Firestore 文档
【发布时间】:2020-11-25 18:25:09
【问题描述】:

我在我的应用程序中使用了 firebase 身份验证和 angularfirestore,但遇到了一个奇怪的问题。如下图所示,在删除 firebase auth 中的用户后,我想删除 firestore 中的用户文档,但 firestore 部分不起作用。

编辑 2:在代码中,this.currentUserangularFireAuth.auth.currentUser 不同。这是我单独创建的局部变量。

return this.angularFireAuth.auth.currentUser.delete().then(() => {
            this.angularFirestore.collection("userData").doc(this.currentUser.email).delete().then(() => {
                this.currentUser = null;
                this.currentUserSubject$.next(this.currentUser);
                this.setAuthenticationDetails({authType: null, rememberMe: false});
                this.storageService.setRememberedUser(null);
            });
        })

但是,如果我先从 firestore 中删除文档,然后再从 firebase auth 中删除用户,它就可以工作。

return this.angularFirestore.collection("userData").doc(this.currentUser.email).delete().then(() => {
            return this.angularFireAuth.auth.currentUser.delete().then(() => {
                this.currentUser = null;
                this.currentUserSubject$.next(this.currentUser);
                this.setAuthenticationDetails({authType: null, rememberMe: false});
                this.storageService.setRememberedUser(null);
            });
        })

有人知道为什么会这样吗?他们都返回承诺,所以我希望行为是相同的。

编辑 1:添加了 catch,但没有打印任何内容

return this.angularFireAuth.auth.currentUser.delete().then(() => {
            return this.angularFirestore.collection("userData").doc(this.currentUser.email).delete().then(() => {
                this.currentUser = null;
                this.currentUserSubject$.next(this.currentUser);
                this.setAuthenticationDetails({authType: null, rememberMe: false});
                this.storageService.setRememberedUser(null);
            }, (error) => {
                console.log(error);
            });
        }, (error) => {
            console.log(error);
        })

编辑 3: 安全规则

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

【问题讨论】:

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


    【解决方案1】:

    你所观察到的对我来说很有意义。

    如果您在 Firestore 上设置了需要经过身份验证的用户才能删除文档的安全规则,那么删除用户帐户后文档将无法删除是有道理的(因为用户不再经过身份验证)。在退出或删除用户帐户之前,您肯定会希望在通过身份验证时做所有事情。

    您应该在代码中添加错误检查,以查明文档删除是否有任何问题。现在,你在 Promise 链上没有任何 catch 回调,所以你永远不会知道是否出了问题。我怀疑由于安全规则拒绝请求,您会在文档删除时看到“权限被拒绝”类型的错误。

    【讨论】:

    • 嗨,我添加了 catch 回调,但在控制台中没有打印任何内容(这很奇怪,因为如果请求没有通过,我预计会出现一些错误?)。另外,我没有任何需要对用户进行身份验证的安全规则
    • 即使您自己没有编写安全规则,它也可能在项目创建时创建如下安全规则。所以要注意这一点。 Daruul 在他的评论中说得很有道理,如果退出安全规则,如果用户没有登录,我们将无法执行操作。 firebase.google.com/docs/firestore/security/get-started
    • 我的安全规则允许所有操作,所以不能这样
    • 请编辑问题以显示您的安全规则。
    • 如果您在文档写入后没有得到某种结果,那么您应该使用 angularfire 提交一个错误,并附上您的具体步骤来重现。
    【解决方案2】:

    我认为发生的事情是在你执行auth.currentUser.delete() 之后,this.currentUser 也会被清除(如果它持有auth.currentUser

    所以您只是删除了没有失败的doc("")。但是没有这样的文件,所以你什么也看不到。

    【讨论】:

    • 那是我的错,我没有说清楚 this.currentUser 是一个不受 auth.currentUser() 影响的局部变量。我已经用 console.log 对其进行了测试,并且 this.currentUser 不为空,因此我问这个。真的很迷茫
    猜你喜欢
    • 1970-01-01
    • 2021-01-13
    • 2020-11-23
    • 1970-01-01
    • 2021-07-01
    • 2020-10-08
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多