Firebase 3.6.0 (Auth) - detecting a specific error with Swift 3.0

I am trying to figure out how to detect a specific error. Say that the login failed, I want to check if there was an error if the specified account does not exist, and then inform the browser. The same thing happens with all other errors, if possible.

In Parse, I would check if the error.code code is equal to a certain number, not sure if it is the same or something similar to Firebase.

+4
source share
2 answers

Use this: -

if let errCode = FIRAuthErrorCode(rawValue: err!._code) {

                switch errCode {
                case .errorCodeInvalidEmail:
                    print("invalid email")
                case .errorCodeEmailAlreadyInUse:
                    print("in use")
                default:
                    print("Other error!")
                }

            }

Where err is the error received from firebase

+6
source

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

if error != nil {

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

        switch errCode {
        case .invalidEmail:

            print("invalid email")
            // Create an alert message
            let alertMessage = UIAlertController(title: "Invalid Email", message: "Please check the entered email address", preferredStyle: .alert)
            // Attach an action on alert message
            alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                alertMessage.dismiss(animated: true, completion: nil)
            }))
            // Display the alert message
            self.present(alertMessage, animated: true, completion: nil)

        case .emailAlreadyInUse:

            print("in use")
            // Create an alert message
            let alertMessage = UIAlertController(title: "Existed Email", message: "The email existed in our database, login instead of registering", preferredStyle: .alert)
            // Attach an action on alert message
            alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                alertMessage.dismiss(animated: true, completion: nil)
            }))
            // Display the alert message
            self.present(alertMessage, animated: true, completion: nil)

            case .weakPassword:

                print("password is weak")
                // Create an alert message
                let alertMessage = UIAlertController(title: "Password is weak", message: "Use upper and lower characters along with numbers", preferredStyle: .alert)
                // Attach an action on alert message
                alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                    alertMessage.dismiss(animated: true, completion: nil)
                }))
                // Display the alert message
                self.present(alertMessage, animated: true, completion: nil)

        default:
            print("Other error!")
        }

    }
}
+2

Source: https://habr.com/ru/post/1656429/


All Articles