2016-12-21 38 views
1

我是Firebase的新手,如果此问题有点“初学者”或令人困惑,我会提前道歉。检查用户ID(Firebase和Swift)

我正在玩一个学习使用Firebase的项目,我有两个用户类型“医生”和“病人”。我已经能够建立一个完美的注册和登录系统。目前,如果您注册成为医生,则可以继续看医生页面,如果您注册成为患者,则可以继续访问患者页面。我还设置了这样的功能,如果您已经登录,那么一旦您启动该应用程序,就可以跳过登录/注册页面并直接进入主页。

我的问题是,如果您作为医生或患者登录,则无关紧要,它会在发布时自动进入患者主页。我正在尝试检查当前用户标识是否是数据库中“患者”节点下的用户标识,然后如果当前用户标识位于数据库中的“医生”节点下,则继续转到患者主页。然后继续到医生主页,否则打印本地错误到控制台。这是我目前坚持的地方,任何帮助或指导将不胜感激。

这是我当前的代码自动塞格斯你主页,如果你是在启动应用程序的登录,这是最初的视图控制器:

 FIRAuth.auth()?.addStateDidChangeListener({ (auth, user) in 

     if user != nil{ 

      print("User is signed in.") 

      //send the user to the PatientHomeViewController 

      let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:nil) 

      let PatientHomeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "PatientHomeViewController") 

      //send the user to the patient homepage 
      self.present(PatientHomeViewController, animated: true, completion: nil) 

     } 
    }) 

在此先感谢堆。

+0

没有足够的代码来帮助。控制器逻辑决定哪个视图进入的视图在哪里?你在哪里加载用户角色以确定他们是耐心还是医生?这将有助于包含所有相关代码,示例数据,调试日志记录和版本信息,以便我们可以充分理解问题。请参阅[如何提问](http://stackoverflow.com/help/how-to-ask)和[创建mcve](http://stackoverflow.com/help/mcve)。 – Kato

回答

1

我已经能够找到一种方法来解决我的问题。我知道这不是最好的解决方案,但经过大量测试后,它运行良好,没有问题。如果其他人有类似的问题,我用来解决我的问题,并能够检查两种类型的用户的代码是:

 let rootRef = FIRDatabase.database().reference() 

    FIRAuth.auth()?.addStateDidChangeListener({ (auth, user) in 

     if(user != nil){ 

      rootRef.child("Patients").child((user?.uid)!).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in 

       if(snapshot.exists()) { 

        print("Patient is signed in.") 

        let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:nil) 

        let patientHomeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "PatientHomeViewController") 

        self.present(patientHomeViewController, animated: true, completion: nil) 

       } else { 

        print("Doctor is signed in.") 

        let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:nil) 

        let doctorHomeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "DoctorHomeViewController") 

        self.present(doctorHomeViewController, animated: true, completion: nil) 

       } 
      }) 

     } 

    }) 

如果任何人有一个更好的方式来写这个代码请随时将其添加为评论。

谢谢。