Firebase授权

时间:2017-02-01 21:40:06

标签: ios swift firebase firebase-authentication

我是Swift和Firebase的新手。我创建了几个我想在帐户创建视图控制器中取消隐藏的标签,例如密码长度必须至少为6个字符,电子邮件无效等等。我不知道如何为此创建if,else语句,或者Firebase已经内置了一些内容。再次,这只适用于Swift,我知道有Android的信息,但我真的不明白。

这是我目前的代码。

@IBAction func nextTapped(_ sender: Any) {

    guard let email = emailField.text, !email.isEmpty else {
        return
    }

    guard let pwd = confirmPass.text, !pwd.isEmpty, confirmPass.text == PassField.text else {
        passMatch.isHidden = false
        return
    }

    FIRAuth.auth()?.createUser(withEmail: email, password: pwd, completion: { (user, error) in
        if error != nil {
            self.emailTaken.isHidden = false
            self.emailTaken.text = FirebaseAuthException().getReason()
        } else {
            if let user = user {
                let userData = ["provider": user.providerID]
                self.completeSignIn(id: user.uid, userData: userData)
            }
        }
    })
}

2 个答案:

答案 0 :(得分:2)

Firebase几乎可以处理所有内容,以下是它们提供的错误列表,您可以使用: https://firebase.google.com/docs/auth/ios/errors#method_specific_error_codes

或者你可以像这里一样展示NSLocalizedDescription

FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
        if let error = error as? NSError{
            if let errorMessage = error.userInfo["NSLocalizedDescription"] as? String {
                //Show Alert with message provided in 'errorMessage'
            } else {
                //Show General Alert
            }
        } else if let user = user {
            //Logged in
        } else {
           // Unknow State
    }

答案 1 :(得分:1)

我实际上已经找到了很多错误处理方法。如果有人感兴趣,并遇到同样的问题,请按照此处。我确信快照有更好的方法,但这样可以解决最基本的问题。

@IBAction func nextTapped(_ sender: Any) {


    guard let email = emailField.text, !email.isEmpty else {
        return
    }

    guard let pwd = confirmPass.text, !pwd.isEmpty, confirmPass.text == PassField.text else {
        passMatch.isHidden = false
        return
    }

    passMatch.isHidden = true

    FIRAuth.auth()?.createUser(withEmail: email, password: pwd, completion: { (user, error) in
        if error != nil {
            if let errorCode = FIRAuthErrorCode(rawValue: (error?._code)!) {
                switch errorCode {
                case .errorCodeNetworkError:
                    print("A network error occurred")
                case .errorCodeInvalidEmail:
                    self.emailTaken.isHidden = false
                case .errorCodeEmailAlreadyInUse:
                    self.emailTaken.isHidden = false
                case .errorCodeWeakPassword:
                    self.passRule.isHidden = false
                default:
                    print("Create User Error: \(error)")
                    }
            }
        } else {
            if let user = user {
                let userData = ["provider": user.providerID]
                self.completeSignIn(id: user.uid, userData: userData)
            }
        }
    })
}