Farasat Niazi
Farasat Niazi

Reputation: 27

UserInfo={NSLocalizedDescription=The email address is already in use by another account., error_name=ERROR_EMAIL_ALREADY_IN_USE}

Hey guys actually i am trying two things here:- trying to create a new account and trying to open a screen like which appears after login but it is showing "email already exist error".

@IBAction func CreateAcccountButton(_ sender: AnyObject) {

guard  let eventInterest = textBox.text,let email = EmailTestfield.text, let password = PasswordTestfield.text, let name = UsernameTestfield.text   else {
    print("Form is not valid")
    return
}

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

    if let error = error {
        print(error)
        return
    }

    guard let uid = user?.uid else {
        return
    }

    //successfully authenticated user
    let imageName = UUID().uuidString
    let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).png")

    if let uploadData = UIImagePNGRepresentation(self.Profilepicture.image!) {
        storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in

            if let error = error {
                print(error)
                return
            }
            print (metadata)
           // let downloadURL = metadata?.downloadURL()
          //  print("URL ", downloadURL)

            if let Profilepictureurl = metadata?.downloadURL()?.absoluteString {
            let values =   ["name": name, "email": email,"EventInterest":eventInterest,"Password":password,"Profilepictureurl": Profilepictureurl ]
                let user = User(dictionary: values as [String : AnyObject])
                let customViewController = MessagesController()
                customViewController.setupNavBarWithUser(user)
                customViewController.fetchUserAndSetupNavBarTitle()
            //    customViewController.navigationItem.title = values["name"] as? String
                self.dismiss(animated: true, completion: nil)
                self.registeruserintoDb(uid,values: values as [String : AnyObject])


            }
        })
    }
}
)
}
fileprivate func registeruserintoDb(_ uid: String, values: [String: AnyObject]) {
    let ref = Database.database().reference()
    let usersReference = ref.child("users").child(uid)

    usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in

        if err != nil {
            print(err!)
            return

        }



    })
}

Upvotes: 0

Views: 1085

Answers (1)

SUPERCILEX
SUPERCILEX

Reputation: 4007

It's exactly what the error says, you already have a user with that email. Instead, use the auth.signIn method and check for currently signed in users.

Upvotes: 0

Related Questions