更新 Firebase 值会出错

Posted

技术标签:

【中文标题】更新 Firebase 值会出错【英文标题】:Updating Firebase values gives an error 【发布时间】:2018-01-07 07:04:28 【问题描述】:

如果我登录或注册,一切都会顺利进行。一旦用户将他们的电子邮件值更改为 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】:

更新电子邮件功能中有很多代码,那么如何使用 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
  

【讨论】:

以上是关于更新 Firebase 值会出错的主要内容,如果未能解决你的问题,请参考以下文章

在android studio中将firebase添加到flutter时出错:插件项目:firebase_core_web未找到。请更新 settings.gradle [重复]

从 UITableView 中删除值会导致崩溃

Swiftui Firebase RTDB 不会在视图中更新

部署 Firebase 云函数时出错

Firebase 选项中未定义存储桶。将个人资料照片更新到 Firebase

在 SwiftUI 上的 Firebase 中使用 updateData 函数时出错