Firebase验证但没有任何内容保存到数据库中

时间:2018-02-19 02:38:41

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

我之前在项目中使用过此代码,并且我能够注册并将所有信息保存到我的firebase数据库中。当我尝试使用新项目进行身份验证时,我会在Firebase的身份验证部分中看到该电子邮件,但是没有任何内容保存到数据库中。我还将我的规则设置为公开,并添加了GoogleService-Info.plist

func submitPressed(){

    var pictureD: Data? = nil
    if let imageView = self.profileImage.image{
        pictureD = UIImageJPEGRepresentation(imageView, 0.4)
    }
    let nameText = self.name.text
    let emailField = self.email.text
    let finalEmail = emailField?.trimmingCharacters(in: .whitespacesAndNewlines)
    let locationInput = self.location.text
    let passwordText = self.password.text
    let biography = self.biography.text
    let phone = self.phoneNumber.text

    if  finalEmail!.isEmpty || pictureD == nil {
        self.view.endEditing(true)
        let alertController = UIAlertController(title: "So Sorry", message: " You must fill all the fields.", preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
        present(alertController, animated: true, completion: nil)
    }else {

        SVProgressHUD.show()
            self.view.endEditing(true)
        self.authService.signUP(firstLastName: nameText!, email: finalEmail!, location: locationInput!, biography: biography!, password: passwordText!, phoneNumber: phoneNumber.text!, pictureData: pictureD! as NSData)

        }

self.loadTabBarController(atIndex: 2)
    SVProgressHUD.dismiss()
}

struct AuthService{

var dataBaseRef: DatabaseReference!{
    return Database.database().reference()
}
var storageRef: StorageReference!{
    return Storage.storage().reference()
}


func signUP(firstLastName: String, email: String, location: String, biography: String, password: String, phoneNumber: String, pictureData: NSData!) {
    SVProgressHUD.show()
    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        if error == nil, let unwrappedUser = user{

            self.setUserInfo(firstLastName: firstLastName, user: unwrappedUser, location: location, phoneNumber: phoneNumber, biography: biography, password: password, pictureData: pictureData)

        }



        else{
            print(error?.localizedDescription)
        }
    }


}

func setUserInfo(firstLastName: String, user: User, location: String, phoneNumber: String, biography: String, password: String, pictureData: NSData!){

    let imagePath = "profileImage\(user.uid)/userPic.jpg"
    let imageRef = storageRef.child(imagePath)

    let metaData = StorageMetadata()
    metaData.contentType = "image/jpeg"
    imageRef.putData(pictureData as Data, metadata: metaData){(newMetaData, error)
        in
        if error == nil{
            let changeRequest = User.createProfileChangeRequest(user)()
            changeRequest.displayName = firstLastName
            if let photoURL = newMetaData!.downloadURL(){
                changeRequest.photoURL = photoURL

            }
            changeRequest.commitChanges(completion: { (error) in
                if error == nil{

                    self.saveUserInfo(firstLastName: firstLastName, user: user, location: location, biography: biography, password: password, phoneNumber: phoneNumber)

                    print("user info set")

                }else{
                    print(error?.localizedDescription)
                }
            })

        }else{
            print(error?.localizedDescription)
        }

    }

}

private func saveUserInfo(firstLastName: String, user: User!, location: String, biography: String, password: String, phoneNumber: String){

    let userInfo = ["name": firstLastName,  "email": user.email!, "password": password, "location": location, "phoneNumber": phoneNumber, "biography": biography, "uid": user.uid, "photoURL": String(describing: user.photoURL!)] as [String : Any]

    let userRef = dataBaseRef.child("users").child(user.uid)
    userRef.setValue(userInfo) { (error, ref) in
        if error == nil{
            print("USER SAVED")





            self.logIn(email: user.email!, password: password)
        }else{
            print(error?.localizedDescription)

        }
    }

}

func logIn(email: String, password: String){

    SVProgressHUD.show()


    Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
        if error == nil {
            if let user = user {
                print("\(user.displayName!) has been signed in")

                SVProgressHUD.dismiss()


                let appDel : AppDelegate = UIApplication.shared.delegate as! AppDelegate
                appDel.logUser()

            }else{
                print(error?.localizedDescription)

            }

        }
    }
}

0 个答案:

没有答案