从Firebase中删除用户帐户。

huangapple go评论68阅读模式
英文:

Delete account from Firebase by user

问题

抱歉,看起来你在点击删除账户按钮时遇到了权限问题,错误信息显示为"Missing or insufficient permissions"。你的 Firebase 规则看起来没有正确配置以允许用户删除他们自己的帐户。你可以尝试以下修改:

将这行代码:

".delete": "auth != null && auth.uid == resource.data.uid"

更改为:

".delete": "auth != null && auth.uid == $userId"

这将确保只有具有匹配用户ID的已认证用户才能删除其自己的帐户。

如果问题仍然存在,请确保你的用户在调用 deleteAccount 函数之前已成功登录,以便 Auth.auth().currentUser 返回有效的用户对象。

请尝试这些更改并查看是否解决了你的权限问题。

英文:

I would need to set up rules for the Firebase database so that a user is able to delete their own account. This is what the function looks like in the swift

 func deleteAccount() {
        guard let user = Auth.auth().currentUser else {
            return
        }
       
        let db = Firestore.firestore()
        db.collection("users").document(user.uid).delete { error in
            if let error = error {
                ProgressHUD.showError(error.localizedDescription)
                return
            }
            
          
            user.delete { error in
                if let error = error {
                    ProgressHUD.showError(error.localizedDescription)
                    return
                }
                
                
                (UIApplication.shared.delegate as! AppDelegate).configureInitialViewController()
            }
        }
    }

and this is what the rules look like

{
  "rules": {
    "users": {
      "$userId": {
        ".read": "auth != null && auth.uid == $userId",
        ".write": "auth != null && auth.uid == $userId",
        ".delete": "auth != null && auth.uid == resource.data.uid"
      }
    }
  }
}

Unfortunately when I click on the delete account button it throws me an error : "Missing or insufficient permissions"

答案1

得分: 3

你正在展示的安全规则适用于实时数据库,而你展示的代码正在访问Firestore。虽然这两个数据库都属于Firebase,但它们是完全独立的,一个数据库的安全规则不适用于另一个数据库。

要解决这个错误,你需要设置Firestore的规则。你可以在Firestore规则文档中找到相关信息。

英文:

The security rules you're showing apply to the Realtime Database, while the code you're showing is accessing Firestore. While both databases are part of Firebase, they're completely separate, and the security rules for one don't apply to the other.

To fix the error, you will have to set the rules for the Firestore.

huangapple
  • 本文由 发表于 2023年5月25日 18:21:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定