Auth.auth()。currentUser?.uid =无iOS Firebase

时间:2018-10-04 11:33:56

标签: ios swift firebase

即使在用户登录后,将变量设置为Auth.auth().currentUser?.uid的值始终会返回nil,我是否错过了Firebase有时使用异步方法的事实?在登录功能之后运行了用于从Firebase实时数据库返回数据的功能,目前该功能使用常量UID进行测试。

typealias RetrieveUserCompletionBlock = ((_ userType: String) -> Void)
func retrieveUserType(withBlock completion: @escaping RetrieveUserCompletionBlock){

    let userTypeDB = Database.database().reference()
    let currentUser = "kLxqZteRfBeC0bNIkLCjrPukMGx1"

    var testUser = Auth.auth().currentUser?.uid
    print(testUser)

    userTypeDB.child("UserType").child(currentUser).observeSingleEvent(of: .value, with: {
        (snapshot) in
        // Get user value
        let value = snapshot.value as? NSDictionary
        let email = value?["Email"] as? String ?? ""
        completion(value?["User Type"] as? String ?? "")
    }){
        (error) in
        completion("default value")
        print(error.localizedDescription)
    }
}

@IBAction func loginButtonPressed(_ sender: Any) {

    SVProgressHUD.show()

    Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in

        if error != nil {
            //error
            if let errorCode = AuthErrorCode(rawValue: error!._code) {

                switch errorCode {

                case .missingEmail:
                    SVProgressHUD.showError(withStatus: "Please enter a email in the text field")
                    SVProgressHUD.dismiss(withDelay: 2)
                case .userDisabled:
                    SVProgressHUD.showError(withStatus: "Your account is disabled")
                    SVProgressHUD.dismiss(withDelay: 2)
                case .invalidEmail:
                    SVProgressHUD.showError(withStatus: "Invalid email, please enter a valid email")
                    SVProgressHUD.dismiss(withDelay: 2)
                case .wrongPassword:
                    SVProgressHUD.showError(withStatus: "Incorrect password")
                    SVProgressHUD.dismiss(withDelay: 2)
                case .userNotFound:
                    SVProgressHUD.showError(withStatus: "Account details not found, please try again")
                    SVProgressHUD.dismiss(withDelay: 2)
                default:
                    print("Error")
                }
            }
        }

        else {
            //success
            SVProgressHUD.showSuccess(withStatus: "Success")
            SVProgressHUD.dismiss(withDelay: 1)
            self.performSegue(withIdentifier: "goToMenuFromLogin", sender: self)
        }
    }
    retrieveUserType { (userType) in
        if userType != "Student"{
            print("error")
        } else {
            print("success")
        }
    }
}

0 个答案:

没有答案