创建新的Firebase用户 - 正确的方法

时间:2016-05-21 21:56:35

标签: swift firebase firebase-authentication

我们对Firebase进行了大量更新,并且我在使用auth系统时遇到了困难。我希望在遇到其他错误时显示警报。

现在我有

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

                    if let error = error {
                        print(error.localizedDescription)

                        if error.code == statusTooShortPassword {
                            Alert(title: "Error", message: "The password must be 6 characters long or more")
                                .showOkay()
                        } else if error.code == statusEmailInvalid {
                            Alert(title: "Error", message: "Invalid email. Try again")
                                .showOkay()
                        } else {
                            print(error)
                            Alert(title: "Error", message: "Try again")
                                .showOkay()
                        }

                    }
                }

但我有强烈的感觉error.code不是处理新用户创建的最佳方式。我正在看这部分文档:

enter image description here

但我不知道如何在FIRAuthErrorCodeInvalidEmail

中实施FIRAuth.auth()?.createUserWithEmail

请有人请提供正确的方法吗?

更新: 如果电子邮件格式无效,我会收到:

Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, print and inspect the error details for more information." UserInfo={NSUnderlyingError=0x7fc3537ceca0 {Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey=<CFBasicHash 0x7fc3537d28c0 [0x1091b5a40]>{type = immutable dict, count = 3,
entries =>
    0 : <CFString 0x7fc3537c8c80 [0x1091b5a40]>{contents = "message"} = <CFString 0x7fc3537d2740 [0x1091b5a40]>{contents = "INVALID_EMAIL"}
    1 : errors = <CFArray 0x7fc3537ba570 [0x1091b5a40]>{type = immutable, count = 1, values = (
    0 : <CFBasicHash 0x7fc3537a9190 [0x1091b5a40]>{type = immutable dict, count = 3,
entries =>
    0 : reason = invalid
    1 : message = <CFString 0x7fc3537e7030 [0x1091b5a40]>{contents = "INVALID_EMAIL"}
    2 : domain = global
}

)}
    2 : code = <CFNumber 0xb000000000001903 [0x1091b5a40]>{value = +400, type = kCFNumberSInt64Type}
}
}}, error_name=ERROR_INTERNAL_ERROR, NSLocalizedDescription=An internal error has occurred, print and inspect the error details for more information.}

我的更新代码:

if error.code == FIRAuthErrorCode.ErrorCodeInvalidEmail.rawValue {
                            Alert(title: "Error", message: "That is some funky email there. Try again")
                                .showOkay()
                        } else if error.code == FIRAuthErrorCode.ErrorCodeWeakPassword.rawValue {
                            Alert(title: "Error", message: "Password is too weak. Try 6 or more characters. Try again")
                                .showOkay()
                        } else {
                            print(error)
                        }

1 个答案:

答案 0 :(得分:3)

我根据@ user2462805更新了 Swift 3 语法的答案

试试这个

FIRAuth.auth()?.createUser(withEmail: email, password: pwd) { (authResult, error) in
        if let error = error {

            if error._code == AuthErrorCode.invalidEmail.rawValue {
                //the error code goes here
            }
        }
    }