【问题标题】:Checking if User Exists in Firebase doesn't Work - Swift检查 Firebase 中是否存在用户不起作用 - Swift
【发布时间】:2020-05-26 12:22:39
【问题描述】:

我在我的 Swift / iOS 项目中使用 Firebase,并尝试实施检查以确定用户在登录 / 登录用户流程中是否已存在于数据库中。

这是我目前用来检查用户是否存在的方法......无论如何,即使我可以直观地验证用户是否在我的数据库中,我总是得到用户不存在的结果.

if let user = Auth.auth().currentUser {
    let ref = self.ref.child("users").child(user.uid)
    ref.observeSingleEvent(of: .value, with: { snapshot in
        self.performSegue(withIdentifier: "GoToMainViewController", sender: nil)
     })

} else {
    self.performSegue(withIdentifier: "GoToProfileCreationViewController", sender: nil)
}

我已经尝试在这两个 SO 线程 herehere 中接受,但始终得到相同的结果。

编辑:这是我的数据库结构

EDIT2:这是完整的流程,包括我对登录用户进行身份验证的代码(通过电话号码)。在我记录用户 ID 的两个地方,我都得到了登录 ID。

Auth.auth().signIn(with: credential) { (authResult, error) in
    if let error = error {
        let alert = UIAlertController(title: nil, message: error.localizedDescription, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { actions in
        })
        self.present(alert, animated: true)
        return
    }

    let userID = Auth.auth().currentUser?.uid

    print(userID)

    Auth.auth().addStateDidChangeListener { auth, user in
        if user != nil{
            print("user is not nil")
            //self.performSegue(withIdentifier: "GoToJoinChannelsViewController", sender: self)
        }
        else{
            print("user is nil")
            //self.performSegue(withIdentifier: "GoToProfileCreationViewController", sender: nil)
        }
    }

    print(userID)

    //Check if user exists
    if let user = Auth.auth().currentUser {
        let ref = self.ref.child("users").child(user.uid)
        ref.observeSingleEvent(of: .value, with: { snapshot in
            print(snapshot)
            //self.performSegue(withIdentifier: "GoToJoinChannelsViewController", sender: nil)
        })

    } else {
        //self.performSegue(withIdentifier: "GoToProfileCreationViewController", sender: nil)
    }

生成的快照如下所示:

Snap (sxQLr6p9meZyQi8p8RPrjVxcUi33) {
    bio = h;
    birthday = "11-Feb-1989";
    firstname = n;
    lastname = n;
}

【问题讨论】:

  • 你能告诉我们你的数据库结构吗?
  • 您用google-cloud-firestore 标记了您的问题,但您问题中的代码访问了实时数据库。虽然这两个数据库都是 Firebase 的一部分,但它们具有完全独立的 API,并且对一个 API 的调用不会看到另一个数据库中的数据。因此,如果您的数据确实存储在 Firestore 中,则需要使用 Firestore API 来访问它。
  • @FrankvanPuffelen 谢谢;刚刚更新了标签
  • 如果您在附加监听器之前print(user.uid),它会显示什么?那么如果你在闭包内print(snapshot.value),它会显示什么?请将代码和输出都添加到您的问题中,以便我们可以确切地看到正在发生的事情。
  • 嗯,看不出您的结构或调用数据库的方式有任何明显问题。抱歉,如果我不明白这一点,但是您确定在进行数据库调用时 currentUser 不是 nil 吗?编辑:刚刚看到@FrankvanPuffelen 指向同一个方向。

标签: ios swift firebase firebase-realtime-database firebase-authentication


【解决方案1】:

您需要将数据库侦听器内部附加到身份验证状态更改的侦听器中,以便它仅在用户通过身份验证后运行:

Auth.auth().addStateDidChangeListener { auth, user in
  if user != nil {
    let ref = self.ref.child("users").child(user.uid)
    ref.observeSingleEvent(of: .value, with: { snapshot in
      if snapshot.exists() {
        // TODO: a user is signed in and registered, navigate to the next screen for them
        self.performSegue(withIdentifier: "GoToJoinChannelsViewController", sender: nil)
      }
      else {           
        // TODO: a user is signed in and not registered, navigate to the next screen for them
      }
    })
  }
  else{
    self.performSegue(withIdentifier: "GoToProfileCreationViewController", sender: nil)
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-15
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多