Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase 3.6.0 (Auth) - Detecting the specific error using Swift 3.0

I'm trying to work out how to detect a certain error. Say the login failed, I want to check if the error was say the account entered doesn't exist, and then tell the viewer that. Same goes with all of the other errors if possible.

In Parse I would check if the error.code was equal to a certain number, not sure if it's the same or anything similar to Firebase.

like image 518
Max Kortge Avatar asked Oct 01 '16 04:10

Max Kortge


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 received error from firebase

like image 171
Dravidian Avatar answered Nov 15 '22 09:11

Dravidian


Here is new format with notification

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!")
        }

    }
}
like image 24
Ahmadiah Avatar answered Nov 15 '22 10:11

Ahmadiah