ios - Swift Firebase -如何阻止电话号码发布到特定引用

标签 ios swift firebase firebase-realtime-database

我的应用程序是一个美食应用程序,我让用户发布食物。在有人发帖之前,会有警告说“仅限食物和饮料,任何其他东西,您将被终身禁止发帖”​​

let dict = ["pictureOfCouchUrl...": "foodPic"]

let postsRef = Database.database().reference().child("posts").child(uid)
postsRef.updateChildValues(dict)

在上面的示例中,该用户发布了一张沙发的照片。我看到了,现在我想禁止该用户在此引用上发布任何内容。

当用户注册我的应用程序时,他们使用他们的电子邮件地址创建一个帐户,但在发布之前他们必须验证他们的电话号码,这最终将他们的电子邮件地址和电话链接到他们的 uid。电话号码仅用于发帖,并使用电子邮件地址登录。

let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationId, verificationCode: verificationCode)

Auth.auth().currentUser?.link(with: credential, completion: { [weak self](authDataResult, error) in ...

我还允许用户完全删除他们的帐户,从而删除他们的电子邮件、电话号码和 uid:

let user = Auth.auth().currentUser
user?.delete { (error) in
    if let error = error { }
}

问题是,如果我想阻止发布沙发的用户的 uid,他们所要做的就是完全删除其帐户,创建一个新帐户,然后获取一个全新的 uid,这意味着此过程可以重复自己。但是,如果我使用他们的电话号码(他们最终必须注册才能发帖)来阻止他们使用 postsRef,那么即使他们创建一个新帐户并获得新的 uid,他们仍然无法使用该电话号码发帖(我没有看到有人更改电话号码只是为了能够在我的应用程序上发帖)。

如何阻止用户使用其电话号码发帖?而不是他们的 uid?

这是我的规则:

"posts": {
  ".read": "auth.uid != null",
    "$uid": {
      ".write": "$uid === auth.uid"
    }
 }

最佳答案

我专门为被阻止的电话号码创建了一个引用:

"blockedNumbers": {
  ".read": "auth.uid != null",
    "$uid": {
      ".write": "$uid === auth.uid"
    }
 }

每当用户的电话号码被阻止时,该号码就会永久保留在该节点中。即使用户删除了他们的帐户,该电话号码也将始终存在其中。在用户发帖之前,我只需检查该电话号码是否在其中,如果是,他们就无法发帖。

let dict = // dict with data that needs to get uploaded

let userPhoneNumber = "+12125551212"

let blockedNumbersRef = Database.database().reference().child("blockedNumbers").child(userPhoneNumber)
postsRef.observeSingleEvent(of: .value, with: { (snapshot) in

    if snapshot.exists() {

        // show alert because this phone number is blocked
        return
    }

    // the phone number doesn't exist so go ahead and post
    let postsRef = Database.database().reference().child("posts").child(uid)
    postsRef.updateChildValues(dict)
})

关于ios - Swift Firebase -如何阻止电话号码发布到特定引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57205064/

相关文章:

iphone - 我如何了解 IOS 应用程序是否进入后台?

ios - 在 Swift 中将两个参数传递给 NSTimer

ios - 如何为自定义 UITableViewCell 类设置初始化程序?

iphone - sectionNameKeyPath 通过多重关系

ios - 如果 tableView 数据为空,如何显示另一个 View Controller

json - 从 TableView 中的 JSON 数据下载可重复使用的单元格或错误的异步图像

firebase - Firebase REST API 和 SDK 客户端有什么区别?以及如何为客户工作?

Swift4 错误无法将类型 '(_) -> Void' 的值转换为预期的参数类型 '(_) -> _'

javascript - 在 Firebase 的有序列表中获取对象的索引

ios - 未在物理设备上获取联系人 - iOS