我可以使用以下代码在firebase中创建新用户。但是,一旦我在底部引入了重复的用户名检查,我就会遇到问题。
方案: 创建具有唯一地址的新用户,但选择已存在的用户名。
流速:
有没有办法在第二次运行之前刷新FIRAuth.auth()
?它就好像之前的“创建用户”#34;尝试被保存在内存中并绊倒第二次执行。
FIRAuth.auth()?.createUserWithEmail(email!, password:pwd!, completion: {
(user, error) in
if error != nil {
switch(error!.code) {
case FIRAuthErrorCode.ErrorCodeEmailAlreadyInUse.rawValue:
self.showErrorAlert("Email Already Exists", msg: "This email address is currently in use.")
default:
self.showErrorAlert("Other Error", msg: "Please contact us with the following error code \(error?.code)")
}
} else {
DataService.ds.REF_USERS.queryOrderedByChild("Username").queryEqualToValue(userName).observeSingleEventOfType(.Value, withBlock: {
snapshot in
if (snapshot.value is NSNull) {
print("The SnapShot Value: \(snapshot.value)")
NSUserDefaults.standardUserDefaults().setValue(user!.uid, forKey: KEY_UID)
let userData=["First Name": firstName!, "Last Name": lastName!, "Email": email!, "Username": userName!, "Age": age!, "Skill": skill!]
DataService.ds.createFirebaseUser(user!.uid, user: userData)
self.performSegueWithIdentifier(NEW_USER_LOGGED_IN, sender: nil)
} else {
self.showErrorAlert("Username Already Exists", msg: "The username is currently in use. Please choose another.")
}
})
答案 0 :(得分:0)
只是为了跟进这一点。我需要在执行“createUserWithEmail”命令之前先检查用户名是否存在。一旦我转移代码以适应,它就按预期工作。