3
votes

I'm having a hard time figuring out why firebase is returning the error code 17011 (which according to Firebase documentation is a user not found error), when creating a user with email and password even after deleting all the users in the database and authentication sections of the firebase console. Although the user is created successfully and added to firebase realtime database the error code keeps popping up. Below is a complete error that gets printed out on the console:

Optional(Error Domain=FIRAuthErrorDomain Code=17011 "There is no user record corresponding to this identifier. The user may have been deleted." UserInfo={NSLocalizedDescription=There is no user record corresponding to this identifier. The user may have been deleted., error_name=ERROR_USER_NOT_FOUND})!

The complete class:

import UIKit
import Firebase

class LoginViewController: UIViewController , UITextFieldDelegate {


@IBOutlet weak var emailField: RoundedCornerTextField!
@IBOutlet weak var passwordField: RoundedCornerTextField!
@IBOutlet weak var segmentedControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()

emailField.delegate = self
passwordField.delegate = self

}

@IBAction func signupLoginBtnTapped(_ sender: Any) {

if emailField.text != nil && passwordField.text != nil {
if let email = emailField.text, let password = passwordField.text {

Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in //Sign in

  if error == nil {

      if let user = user {

          if self.segmentedControl.selectedSegmentIndex == 0 {

              let userData = ["provider": user.providerID] as [String: Any]

              DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)

          } else {   

              let userData = ["provider": user.providerID] as [String: Any]

              DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
          }
      }

      print ("Firebase: Success authentication with Firebase.")

      self.dismiss(animated: true, completion: nil)

  } else {
   if let errorCode = AuthErrorCode(rawValue: error!._code){
switch errorCode {
 case.wrongPassword:
        self.showAlert("You entered an invalid password please try again!")
    default:
            self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
          print("Creating user error 1 \(error.debugDescription)!")
       }
   }

  Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in //creating a user

      if error == nil {
          if let user = user {
              if self.segmentedControl.selectedSegmentIndex == 0 { 
                  let userData = ["provider": user.providerID] as [String: Any]
                  DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
              } else { 
                  let userData = ["provider": user.providerID] as [String: Any]
                  DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
              }
      } else {
              if let errorCode = AuthErrorCode(rawValue: error!._code) {
                  switch errorCode {
                  case .invalidEmail:
                      self.showAlert("You entered an invalid email!")
                  case .userNotFound:
                      self.showAlert("User not found")
                  default:
                      print("Creating user error 2 \(error.debugDescription)!")
                      self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
                  }
              }
          }
          print("Firebase: user created successfully")
          self.dismiss(animated: true, completion: nil)
             }
           })
         }
      })
   }
 }
}

}

And block of code that the error occurs:

Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in 
if error == nil {
    if let user = user {
        if self.segmentedControl.selectedSegmentIndex == 0 {
            let userData = ["provider": user.providerID] as [String: Any]
            DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)   
        }   
        else { 
                let userData = ["provider": user.providerID]
                DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
             }
    }

    print ("Firebase: Success authentication with Firebase.")

    self.dismiss(animated: true, completion: nil)

}     
else {

    if let errorCode = AuthErrorCode(rawValue: error!._code){   
       switch errorCode {
            case.wrongPassword:
                self.showAlert("You entered an invalid password please try again!")
            default:
                self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
                print("Creating user error \(error.debugDescription)!")
       }
    }

    Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in 
       if error == nil {
           if let user = user { 
               if self.segmentedControl.selectedSegmentIndex == 0 { 
                   let userData = ["provider": user.providerID] as [String: Any]
                   DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)   
                } 
               else { 
                   let userData = ["provider": user.providerID]
                   DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
                }

            } 
            else {
               if let errorCode = AuthErrorCode(rawValue: error!._code) {
                   switch errorCode { 
                        case .invalidEmail:
                            self.showAlert("You entered an invalid email!")
                        case .userNotFound:
                            self.showAlert("User not found")
                        default:
                            print("Creating user error \(error.debugDescription)!")
                            self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
                    }
               }
           }
           self.dismiss(animated: true, completion: nil)
       }

The error occurs in the Auth.auth().signIn else block below:

Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in 
   if error == nil {
       if let user = user { 
           if self.segmentedControl.selectedSegmentIndex == 0 { 
               let userData = ["provider": user.providerID] as [String: Any]
               DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)   
            } 
           else { 
               let userData = ["provider": user.providerID]
               DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
            }

        } 
        else {
           if let errorCode = AuthErrorCode(rawValue: error!._code) {
               switch errorCode { 
                    case .invalidEmail:
                        self.showAlert("You entered an invalid email!")
                    case .userNotFound:
                        self.showAlert("User not found")
                    default:
                        print("Creating user error \(error.debugDescription)!")
                        self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
                }
           }
       }
       self.dismiss(animated: true, completion: nil)
   }

Could anyone provide a solution to resolve?

P.S Yes I imported firebase to the project and pods are up to date also email/password sign in method in the authentication section of firebase console is enabled.

1
If you create a user in the authentication tab of the Firebase console can they login?DoesData
@DoesData Yes they can login with no problem at all9it3e1
Are your email and password correct?3stud1ant3
@3stud1ant3 yes they are problem occurs when creating a new users. But If I quit the app and restart again and try login in with the same email and password that previously shows the error it logs in with no problem. And when I create a user from the firebase console they too can log in. The error only occurs when trying to create a new user. Even though the user is created and stored in the database.9it3e1
I think problem is that when you create user, you try to sign in without signing out , because when you create a user you are already signed in3stud1ant3

1 Answers

1
votes

After looking at the code for a while, I released that the all the errors handling sign in should be handled before entering the createUser() function. And since there is .userNotfound error in Firebase Auth. calling the createUser() in the case.userNotFound solves the problem as shown below:

Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in

        if error == nil {
            if let user = user {
                if self.segmentedControl.selectedSegmentIndex == 0 {
                    let userData = ["provider": user.providerID] as [String: Any]
                    DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
                } else {
                    let userData = ["provider": user.providerID]
                    DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
                }
            }

            print ("Firebase: Success authentication with Firebase.")

            self.dismiss(animated: true, completion: nil)

        } else {

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

                switch errorCode {

                case.wrongPassword:
                    self.showAlert("You entered an invalid password please try again!")

                case.userNotFound:
                    Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
                        if error == nil {
                            if let user = user {
                                if self.segmentedControl.selectedSegmentIndex == 0 {
                                    let userData = ["provider": user.providerID] as [String: Any]
                                    DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
                                } else {
                                    let userData = ["provider": user.providerID]
                                    DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
                                }

                            } else {
                                if let errorCode = AuthErrorCode(rawValue: error!._code) {

                                    switch errorCode {

                                    case .invalidEmail:
                                        self.showAlert("You entered an invalid email!")

                                    case .userNotFound:
                                        self.showAlert("User not found")

                                    default:
                                        print("Creating user error 2 \(error.debugDescription)!")
                                        self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
                                    }
                                }
                            }

                            self.dismiss(animated: true, completion: nil)
                        }
                    })

                default:
                    self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
                    print("Creating user error \(error.debugDescription)!")
                }
            }
        }
 })