Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Firebase Auth Errors? / Swift / Firebase

Hey guys can someone tell me how to handle this? I tried a lot but if I correct the one error another one is appearing... Thanks in advance

Auth.auth().createUser(withEmail: eMailTextField.text!, password: passwordTextField.text!) { (data, error) in
            if error != nil {
                if let errCode = error as NSError? {
                    
                    guard let errorCode = AuthErrorCode(rawValue: error) else {
                                    print("there was an error logging in but it could not be matched with a firebase code")
                                    return

                                }
                    
                    switch errorCode {
                        case .FIRAuthErrorCodeNetworkError:
                            print("No Internet Connection")
                        case .ErrorCodeEmailAlreadyInUse:
                            print("in use")
                        default:
                            print("Create User Error: \(error!)")
                    }
                    
            }
                
            } else {
                print("all good... continue")
                        }

1 Answers

You can bridge to NSError and then create the AuthErrorCode based on error.code:

Auth.auth().createUser(withEmail: "MyEmail", password: "MyPassword") { authResult, error in
            if error != nil, let error = error as NSError? {
                if let errorCode = AuthErrorCode(rawValue: error.code) {
                    switch errorCode {
                    case .invalidEmail:
                        break
                    case .emailAlreadyInUse:
                        break
                    default:
                        break
                    }
                }
            } else {
                //no error
            }
        }

Note that I only listed a couple of the errorCode possibilities - there's quite an extensive list of them.

like image 115
jnpdx Avatar answered Jul 01 '26 19:07

jnpdx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!