2016-12-21 46 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) 

       } 
      }) 

     } 

    }) 

如果任何人有一個更好的方式來寫這個代碼請隨時將其添加爲評論。

謝謝。