创建用户Swift时的Firebase错误处理

时间:2016-11-08 10:35:06

标签: swift firebase error-handling firebase-authentication

我目前正在开发iOS应用程序,我想实现Firebase数据库。

我在用户创建过程中处理错误时遇到问题。我总是从交换机获得默认错误,这是我的代码:

FIRAuth.auth()?.createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
        if (error != nil) {

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

                var alertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.alert)
                let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                    (result: UIAlertAction) -> Void in
                    print("Error transmitted")
                }

                switch errCode {
                case .errorCodeInvalidEmail:
                    print("Invalid email")
                    alertController = UIAlertController(title: "Error", message: "Email syntax is not correct", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(okButton)
                    self.present(alertController, animated: true, completion: nil)
                case .errorCodeEmailAlreadyInUse:
                    print("Email already in use")
                    alertController = UIAlertController(title: "Error", message: "This email is already in use", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(okButton)
                    self.present(alertController, animated: true, completion: nil)
                case .errorCodeWeakPassword:
                    print("Password weak")
                    alertController = UIAlertController(title: "Error", message: "Password is too weak. Please choose a password which contains at least 6 characters.", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(okButton)
                    self.present(alertController, animated: true, completion: nil)
                default:
                    // ALWAYS GET HERE.
                    print(error)
                    alertController = UIAlertController(title: "Error", message: "An unknown error occured.", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(okButton)
                    self.present(alertController, animated: true, completion: nil)
                }

            }

        } else {
            print("User created")
            let newUser = ["email": self.emailField.text!]
            let firebaseNewUser = self.ref.childByAutoId()
            firebaseNewUser.setValue(newUser)
        }

此外,打印(错误)显示:

Optional(Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, 
    print and inspect the error details for more information." 
    UserInfo={NSUnderlyingError=0x170257be0 {
        Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" 
        UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey={
    code = 400;
    errors =     (
            {
        domain = usageLimits;
        message = "Bad Request";
        reason = keyExpired;
    }
    );
    message = "Bad Request";
}}}, 
error_name=ERROR_INTERNAL_ERROR, 
NSLocalizedDescription=An internal error has occurred, print and inspect the 
    error details for more information.})

有人能帮助我吗?

3 个答案:

答案 0 :(得分:0)

尝试在Google控制台中启用身份工具包API。

答案 1 :(得分:0)

实际上,(回答上一条消息),身份工具包API已经被激活。

问题来自这样一个事实,即API密钥已生成但受限于网站请求。

解决方案:所以我只生成了另一个API密钥,将其限制在iOS应用程序中,再次下载.plist,将其放入项目并在其中添加API_KEY(API_KEY为信息属性作为新生成的api密钥值)

答案 2 :(得分:0)

对于创建的新用户请按照这个简单的代码。请将此代码放在Create Button @IBAction函数中。

FIRAuth.auth()?.createUserWithEmail(Username.text!, password: Password.text!
            , completion: {

                user , error in

                if error != nil {
                 print("Something Went to Wrong :(")
                }

                else {

                    print("Account Created Sucessfully ")
                }


        })