我很难弄清楚为什么firebase会返回错误代码 17011 (根据Firebase文档是用户未找到错误),创建用户甚至是电子邮件和密码时删除firebase控制台的数据库和身份验证部分中的所有用户之后。虽然用户已成功创建并添加到firebase实时数据库,但错误代码仍会弹出。以下是在控制台上打印出的完整错误:
可选(错误域= FIRAuthErrorDomain代码= 17011“没有与此标识符对应的用户记录。用户可能已被删除。”UserInfo = {NSLocalizedDescription =没有与此标识符对应的用户记录。用户可能有已被删除。,error_name = ERROR_USER_NOT_FOUND})!
完整的课程:
import UIKit
import Firebase
class LoginViewController: UIViewController , UITextFieldDelegate {
@IBOutlet weak var emailField: RoundedCornerTextField!
@IBOutlet weak var passwordField: RoundedCornerTextField!
@IBOutlet weak var segmentedControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
emailField.delegate = self
passwordField.delegate = self
}
@IBAction func signupLoginBtnTapped(_ sender: Any) {
if emailField.text != nil && passwordField.text != nil {
if let email = emailField.text, let password = passwordField.text {
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in //Sign in
if error == nil {
if let user = user {
if self.segmentedControl.selectedSegmentIndex == 0 {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
} else {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
}
}
print ("Firebase: Success authentication with Firebase.")
self.dismiss(animated: true, completion: nil)
} else {
if let errorCode = AuthErrorCode(rawValue: error!._code){
switch errorCode {
case.wrongPassword:
self.showAlert("You entered an invalid password please try again!")
default:
self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
print("Creating user error 1 \(error.debugDescription)!")
}
}
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in //creating a user
if error == nil {
if let user = user {
if self.segmentedControl.selectedSegmentIndex == 0 {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
} else {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
}
} else {
if let errorCode = AuthErrorCode(rawValue: error!._code) {
switch errorCode {
case .invalidEmail:
self.showAlert("You entered an invalid email!")
case .userNotFound:
self.showAlert("User not found")
default:
print("Creating user error 2 \(error.debugDescription)!")
self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
}
}
}
print("Firebase: user created successfully")
self.dismiss(animated: true, completion: nil)
}
})
}
})
}
}
}
}
发生错误的代码块:
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
if let user = user {
if self.segmentedControl.selectedSegmentIndex == 0 {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
}
else {
let userData = ["provider": user.providerID]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
}
}
print ("Firebase: Success authentication with Firebase.")
self.dismiss(animated: true, completion: nil)
}
else {
if let errorCode = AuthErrorCode(rawValue: error!._code){
switch errorCode {
case.wrongPassword:
self.showAlert("You entered an invalid password please try again!")
default:
self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
print("Creating user error \(error.debugDescription)!")
}
}
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
if let user = user {
if self.segmentedControl.selectedSegmentIndex == 0 {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
}
else {
let userData = ["provider": user.providerID]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
}
}
else {
if let errorCode = AuthErrorCode(rawValue: error!._code) {
switch errorCode {
case .invalidEmail:
self.showAlert("You entered an invalid email!")
case .userNotFound:
self.showAlert("User not found")
default:
print("Creating user error \(error.debugDescription)!")
self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
}
}
}
self.dismiss(animated: true, completion: nil)
}
错误发生在下面的Auth.auth()。signIn else块中:
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
if let user = user {
if self.segmentedControl.selectedSegmentIndex == 0 {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
}
else {
let userData = ["provider": user.providerID]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
}
}
else {
if let errorCode = AuthErrorCode(rawValue: error!._code) {
switch errorCode {
case .invalidEmail:
self.showAlert("You entered an invalid email!")
case .userNotFound:
self.showAlert("User not found")
default:
print("Creating user error \(error.debugDescription)!")
self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
}
}
}
self.dismiss(animated: true, completion: nil)
}
有人能提供解决方案吗?
P.S是的我将firebase导入项目并且pod是最新的,并且启用了firebase控制台的身份验证部分中的电子邮件/密码登录方法。
答案 0 :(得分:1)
在查看代码一段时间之后,我发布了在进入createUser()函数之前应该处理所有处理登录的错误。由于Firebase Auth中存在.userNotfound错误。在case.userNotFound中调用createUser()解决了如下所示的问题:
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
if let user = user {
if self.segmentedControl.selectedSegmentIndex == 0 {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
} else {
let userData = ["provider": user.providerID]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
}
}
print ("Firebase: Success authentication with Firebase.")
self.dismiss(animated: true, completion: nil)
} else {
if let errorCode = AuthErrorCode(rawValue: error!._code) {
switch errorCode {
case.wrongPassword:
self.showAlert("You entered an invalid password please try again!")
case.userNotFound:
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
if let user = user {
if self.segmentedControl.selectedSegmentIndex == 0 {
let userData = ["provider": user.providerID] as [String: Any]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData)
} else {
let userData = ["provider": user.providerID]
DataService.instance.createFirebaseDBUser(uid: user.uid, userData: userData, isDriver: true)
}
} else {
if let errorCode = AuthErrorCode(rawValue: error!._code) {
switch errorCode {
case .invalidEmail:
self.showAlert("You entered an invalid email!")
case .userNotFound:
self.showAlert("User not found")
default:
print("Creating user error 2 \(error.debugDescription)!")
self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
}
}
}
self.dismiss(animated: true, completion: nil)
}
})
default:
self.showAlert("Unexpected error \(errorCode.rawValue) please try again!")
print("Creating user error \(error.debugDescription)!")
}
}
}
})