Ben Ben - 2 days ago 4
iOS Question

Reading Firebase Auth Error Thrown (Firebase 3.x and Swift)

I'm relatively new to Swift and Firebase. Any help would be greatly appreciated on the following.

I'm having trouble figuring out how to read the FIRAuthErrorNameKey in the new version of Firebase. The following is what I have so far, but the "let errorCode = FIRAuthErrorNameKey" line is incorrect. From reading the Firebase documentation I also tried accessing the error code from the userInfo, but was unsuccessful and am out of ideas.

// Send request to Firebase to add user to register user
FIRAuth.auth()?.createUserWithEmail(emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in

// Check for errors and respond to user accordingly.
if error != nil {

let errorCode = FIRAuthErrorNameKey

switch errorCode {

case "FIRAuthErrorCodeEmailAlreadyInUse":

// Add logic accordingly

case ...:

// Add logic accordingly

case default:

// Add logic accordingly
}
}
})

Answer

Try this. This works for me. Also, after pasting this into your project. If you need to see all the FIRAuthErrorCode codes. Hover your mouse over .ErrorCodeInvalidEmail then press your left mouse button and it will show you the rest.

If you have any problems let me know and ill try to help you. Good luck!

        FIRAuth.auth()?.createUserWithEmail(email, password: password) { (user, error) in

            if error != nil {

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

                    switch errCode {
                        case .ErrorCodeInvalidEmail:
                            print("invalid email")
                        case .ErrorCodeEmailAlreadyInUse:
                            print("in use")
                        default:
                            print("Create User Error: \(error!)")
                    }
                }

            } else {
                print("all good... continue")
            }
        }
Comments