Handling Errors in New Firebase and Swift

Updated for Swift 4 + Firebase 4 + UIAlertController

extension AuthErrorCode {
    var errorMessage: String {
        switch self {
        case .emailAlreadyInUse:
            return "The email is already in use with another account"
        case .userNotFound:
            return "Account not found for the specified user. Please check and try again"
        case .userDisabled:
            return "Your account has been disabled. Please contact support."
        case .invalidEmail, .invalidSender, .invalidRecipientEmail:
            return "Please enter a valid email"
        case .networkError:
            return "Network error. Please try again."
        case .weakPassword:
            return "Your password is too weak. The password must be 6 characters long or more."
        case .wrongPassword:
            return "Your password is incorrect. Please try again or use 'Forgot password' to reset your password"
        default:
            return "Unknown error occurred"
        }
    }
}


extension UIViewController{
    func handleError(_ error: Error) {
        if let errorCode = AuthErrorCode(rawValue: error._code) {
            print(errorCode.errorMessage)
            let alert = UIAlertController(title: "Error", message: errorCode.errorMessage, preferredStyle: .alert)

            let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)

            alert.addAction(okAction)

            self.present(alert, animated: true, completion: nil)

        }
    }

}

Usage example:

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

        if error != nil {
            print(error!._code)
            self.handleError(error!)      // use the handleError method
            return
        }
        //successfully logged in the user

    })

I've actually just struggled with this for quite a bit of time and found what the issue was. I've tried the code posted in an answer above and the error.code line gave me an error. It did work with error._code though. In other words, credit for the original answer to Paul with a slight modificaiton. Here's my final code (I will edit it for all errors though):

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

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

Even though this has been answered correctly, wanted to share a nice implementation for this we added to our project.

This can be done for other error types as well, but we just needed it for the FIRAuthErrorCodes.

If you extend FIRAuthErrorCode to have a variable errorMessage of type string, you can have your own error messages for the users:

extension FIRAuthErrorCode {
    var errorMessage: String {
        switch self {
        case .errorCodeEmailAlreadyInUse:
            return "The email is already in use with another account"
        case .errorCodeUserDisabled:
            return "Your account has been disabled. Please contact support."
        case .errorCodeInvalidEmail, .errorCodeInvalidSender, .errorCodeInvalidRecipientEmail:
            return "Please enter a valid email"
        case .errorCodeNetworkError:
            return "Network error. Please try again."
        case .errorCodeWeakPassword:
            return "Your password is too weak"
        default:
            return "Unknown error occurred"
        }
    }
}

You could customize only some as we have above and group the rest under "Unknown error".

With this extension you can handle an error as shown in Vladimir Romanov's answer:

func handleError(_ error: Error) {
    if let errorCode = FIRAuthErrorCode(rawValue: error._code) {
        // now you can use the .errorMessage var to get your custom error message
        print(errorCode.errorMessage)
    }
}