AlexGarlock
AlexGarlock

Reputation: 83

Firebase Check if user already exists

I tried running the following and it does not work. What am I doing wrong to check the Firebase server if the user has already used the email?

           FIRAuth.auth()!.createUser(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!) {(createUser, error) in
        if error != nil { 
            FIRAuth.auth()!.signIn(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!)

            //Display logged in

            let viewController = self.storyboard!.instantiateViewController(withIdentifier: "TabBarController") as UIViewController
            self.present(viewController, animated: true, completion: nil)

        }else {
            //Display an alert message
            self.displayMyAlertMessage(userMessage: "Email already in use. Please see the login page.")
            return
        }
    }

Upvotes: 0

Views: 3739

Answers (2)

Hitesh Surani
Hitesh Surani

Reputation: 13577

Please refer below code.

Code:

FIRAuth.auth()?.createUser(withEmail: email, password: password) {
  (user, error) in
  if (error) {
    // Email is already in use.
  } else {
    // Create new user successfully
  }
}

Handle Firebase iOS Auth Errors

Upvotes: 4

eshirima
eshirima

Reputation: 3867

Firebase already does this for you. Erase the sign in method and just run the create user method using an email that already exists and FIRAuthErrorCodeEmailAlreadyInUse will be returned. Check this out for more info

Upvotes: 2

Related Questions