0
votes

I have coded a service use to sign-in users:

func login(email: String, password: String) -> Bool {

    var userIsConnected = false

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

        if error != nil {

            print(type(of: error)) // Print 'NSError'
            print(error!)
        }

        else {

            userIsConnected = true
        }
    }
    return userIsConnected
}

When I print the error I get:

Error Domain=FIRAuthErrorDomain Code=17008 "The email address is badly formatted." UserInfo={NSLocalizedDescription=The email address is badly formatted., error_name=ERROR_INVALID_EMAIL}

How can I get the Code value (17008) to be able to do some custom behaviour ?

Note: In a previous version of FirebaseAuth we can simply do error.code but in the last version we can't.

2

2 Answers

2
votes

The type of error is Error and Error can be casted to NSError.

if let error = error, (error as NSError).code == 17008 {
    // do something
}
0
votes

TRY error?.localizedDescription

    // to show error through alert controller
    let alertController = UIAlertController(title: "your title", message: error?.localizedDescription, preferredStyle: .alert)

    let defaultAction = UIAlertAction(title: AlertActionTitle, style: .cancel, handler: nil)
    alertController.addAction(defaultAction)
    self.present(alertController, animated: true, completion: nil)