【问题标题】:Firebase error handling while creating user Swift创建用户 Swift 时的 Firebase 错误处理
【发布时间】:2017-03-21 22:45:47
【问题描述】:

我目前正在开发一个 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.})

有人可以帮帮我吗?

【问题讨论】:

    标签: swift firebase error-handling firebase-authentication


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      问题出在 API 密钥已生成但受限于网站请求这一事实。

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

      【讨论】:

        【解决方案3】:

        对于创建的新用户请按照这个简单的代码。请将这段代码放在 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 ")
                        }
        
        
                })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-06
          • 1970-01-01
          • 1970-01-01
          • 2018-04-18
          • 2016-12-27
          • 2018-08-19
          • 2018-06-25
          • 1970-01-01
          相关资源
          最近更新 更多