gpt4 book ai didi

swift 4 : Firebase still creating user even on error?

转载 作者:行者123 更新时间:2023-11-30 11:51:26 24 4
gpt4 key购买 nike

我有一个在按下注册按钮时运行的函数,该函数使用 Auth.auth().createUser(withEmail: email,password:password) 来注册用户(如果用户不注册)已经有一个帐户,或者没有错误。然后我将它们添加到数据库中并登录。如果有任何错误,我会显示错误并返回

但是,即使用户以前从未注册过并且数据库没有其信息,我也会收到 .emailAlreadyInUse 错误,之后用户仍然会添加到数据库中。 Firebase 似乎正在创建帐户,然后给我一个错误。这是为什么?我该如何解决它?

这是我的代码:

@IBAction func registerPressed(_ sender: Any?) {

//set up new user using Firebase
if (firstNameTextField.text! == "" && emailTextField.text! == "" && passwordTextField.text! == "") {
self.topMessage.text = "Please enter your details"
} else if (firstNameTextField.text! == "" && emailTextField.text! != "" && passwordTextField.text! != "") {
self.topMessage.text = "Please enter your first name"
} else {
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) {
(user, error) in
if error != nil {
print(error!)
if let errCode = AuthErrorCode(rawValue: error!._code) {
switch errCode {
case .weakPassword:
self.topMessage.text = "Password too short"
break
case .emailAlreadyInUse:
self.topMessage.text = "Email already in use"
break
case .invalidEmail:
self.topMessage.text = "Invalid email"
break
case .missingEmail:
self.topMessage.text = "Please enter an email"
break
default:
self.topMessage.text = "Error :( Please try again!"
break
}
}
return
}
else if let user = user {

let username = self.firstNameTextField.text!
let name = self.firstNameTextField.text!
let email = self.emailTextField.text!
let profileImage = ""
let password = self.passwordTextField.text!

let newUser = User(uid: user.uid, username: username, name: name, profileImage: profileImage, email: email, userCoins: 0, questsComplete: 0, rewardsRedeemed: 0, streak: 0, isSubscribed: false)

newUser.save(completion: { (error) in
if error != nil {
print(error!)
} else {
//successfully initialised
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if let error = error {
print(error)
} else {
self.performSegue(withIdentifier: "registerToMainMap", sender: self)
}
})

}
})

}

}
}
}

最佳答案

根据文档Firebase documentation on errors

FIRAuthErrorCodeEmailAlreadyInUse Indicates the email used to attempt sign up already exists. Call fetchProvidersForEmail to check which sign-in mechanisms such user used, and prompt the user to sign in with one of those.

那么,这样的事情会给你带来什么?

Auth.auth().fetchProviders(forEmail: emailTextField.text!) { (email,completion) in
print("email is ", email!)
print("completion is ", completion!)
}

关于 swift 4 : Firebase still creating user even on error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48289850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com