Alex Barbulescu
Alex Barbulescu

Reputation: 331

How To Fetch Error Code From Firebase?

        Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!)
    { (user, error) in
        
        if error != nil {
            print(error!)
            self.warningLabel.isHidden = false;
            self.passwordTextField.text = "";
        } else {
            print("Log in succesful")
            self.performSegue(withIdentifier: "welcomeSeg", sender: self)
        }
    }

Whenever I sign in or sign up a user I just print a generic warning label instead of the actual issue. I print the error I receive and it's too verbose to show to the user.

Error Domain=FIRAuthErrorDomain Code=17009 "The password is invalid or the user does not have a password." UserInfo={NSLocalizedDescription=The password is invalid or the user does not have a password., error_name=ERROR_WRONG_PASSWORD}

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

Is there any way to fetch the error code so I can be more specific with my error messages? I've looked through the documentation but have been unsuccessful in coming up with anything.

Upvotes: 0

Views: 1122

Answers (1)

Tyler Poland
Tyler Poland

Reputation: 52

I would recommend creating an AuthErrorCode object (provided by the Firebase SDK) from the error you receive and using that as you see fit. If I remember correctly, AuthErrorCode is an enum with cases like .wrongPassword, .invalidEmail, etc.

A simple pseudocode example:

if error != nil {
    if let error = AuthErrorCode(rawValue: error?.code) {
        switch error {
        case .wrongPassword:
            // do some stuff with the error code
        }
}

Also, I feel your pain. I've found that the Swift SDK documentation lags quite a bit when changes come along.

Upvotes: 2

Related Questions