更新Firebase值会导致错误

时间:2018-01-07 07:04:28

标签: ios swift firebase

如果我登录或注册,一切运行顺利。一旦用户将其电子邮件值更改为firebase然后注销,我的登录功能就不再有效。我不确定究竟是什么造成了这种情况,但它必须是我的updateEmailAction函数中的内容。

//在更新电子邮件之前正常工作。电子邮件更新和连续SVProgress旋转后不再有效。

@IBAction func signInButton(_ sender: Any) {

    self.view.endEditing(true)
    let email = emailText.text!.lowercased()
    let finalEmail = email.trimmingCharacters(in: .whitespacesAndNewlines)
    let password = passwordText.text!

    if finalEmail.characters.count < 8 || finalEmail.isEmpty || password.isEmpty {

        let alertController = UIAlertController(title: "OOPS", message: "You must fill in all the fields", preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        present(alertController, animated: true, completion: nil)

    }else {
      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()

                    self.enter()
             self.performSegue(withIdentifier: "signInHome", sender: nil)

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

}

//更新电子邮件功能,电子邮件更改成功,然后在注销时无法重新登录。

 @IBAction func updateEmailAction(_ sender: Any) {

    var pictureD: Data? = nil
    if let imageView = self.profileImage.image{
        pictureD = UIImageJPEGRepresentation(imageView, 0.70)
    }

    let emailField = emailText.text?.lowercased()
    let finalEmail = emailField?.trimmingCharacters(in: .whitespacesAndNewlines)

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

        let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()

        //changeRequest. = firstLastName
        changeRequest?.commitChanges(completion: { (error) in
                    if error == nil{

                        let user = Auth.auth().currentUser


                        let userInfo = ["firstLastName": self.name,  "email": finalEmail, "password": self.passwordOld, "location": self.locationOld, "interests": self.interestsOld, "biography": self.biog, "uid": self.uid, "photoURL": self.photoUrl]

                        let userRef = self.dataBaseRef.child("users").child((user?.uid)!)
                        userRef.setValue(userInfo)

                        print("user info set")

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

    }}



 @IBAction func logOutAction(_ sender: Any) {



    if Auth.auth().currentUser != nil {
        // there is a user signed in
        do {
            try? Auth.auth().signOut()

          self.performSegue(withIdentifier: "login", sender: self)


        }
    }


}

1 个答案:

答案 0 :(得分:0)

更新电子邮件功能中有很多代码,那么如何使用这样的Firebase updateEmail函数来简化它;

var finalEmail = ""
//capture a new email from the user

Auth.auth().currentUser?.updateEmail(to: finalEmail) { (error) in
  // ...
}

请注意,要设置用户的电子邮件地址,用户必须最近登录。如果他们没有错误将是

  

FIRAuthErrorCodeCredentialTooOld

您将需要重新验证用户

let thisUser = Auth.auth().currentUser
var cred: AuthCredential

thisUser?.reauthenticate(with: cred) { error in
  if let error = error {
    // Handle the error
  } else {
    // User has re-authenticated
  }
}