Not finding usernames already in the Firebase Database with Swift - ios

I am trying to check my Firebase database to see if there is already a username taken within the database. Unfortunately, when I use a username which is already saved in there, it doesn't let me know. Code below.
#objc func pushedToRegister(_ sender: SignInSignUpButtons) {
checkUserNameAlreadyExist()
Auth.auth().createUser(withEmail: email.text!, password: password.text!, completion: { (user, error) in
if error != nil {
self.errorMessage = (error?.localizedDescription)!
print(error!)
self.showErrorView(forReason: 0)
return
}
else {
print("Registration was successful!")
//Here we should go back to the home screen where the message at the top should say welcome back USER!
if let userID = user?.user.uid {
//Create User Profile
let databaseRef = Database.database().reference()
let usersRef = databaseRef.child("Users").child(userID)
let usernameValue = ["username":self.username.text]
usersRef.updateChildValues(usernameValue, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!.localizedDescription)
self.showErrorView(forReason: 2)
return
}
//Profile created and updated!
self.navigationController?.popViewController(animated: true)
})
}
}
})
}
func checkUserNameAlreadyExist() {
let ref = Database.database().reference()
var usernameTaken = false
ref.child("Users").queryOrdered(byChild: "username").queryEqual(toValue: username.text!).observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists(){
usernameTaken = true
print("username taken")
}else{
usernameTaken = false
print("username available")
}
}) { error in
print(error.localizedDescription)
}
if usernameTaken == false{
//do stuff with unique username
}
}
Unfortunately, every time I type in the same username, it lets me create a new profile everytime. It does not alert me to the fact that the username is already taken.

Firebase functions are asynchronous, so they do not block the rest of the code from running. If you want your code to wait for a function to complete, one option is use closures. You can check out my blog post on closures to see some examples.
Also, the code shown doesn't actually do anything to prevent the rest of the function from running. You need to write some sort of condition to handle that. For example, you could use a boolean in your completion handler, like this:
func checkUserNameAlreadyExist(completion: #escaping (Bool) -> Void) {
let ref = Database.database().reference()
ref.child("Users").queryOrdered(byChild: "username").queryEqual(toValue: username.text!).observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists() {
usernameTaken = true
print("username taken")
completion(true)
} else {
usernameTaken = false
print("username available")
completion(false)
}
}) { error in
print(error.localizedDescription)
completion(true)
}
}
Then in pushToRegister, you check if the boolean is true before proceeding.
#objc func pushedToRegister(_ sender: SignInSignUpButtons) {
checkUserNameAlreadyExist() { isTaken in
if (isTaken == true) {
// show some message to the user
return
}
Auth.auth().createUser(withEmail: email.text!, password: password.text!, completion: { (user, error) in
if let error = error {
self.errorMessage = error.localizedDescription
print(error)
self.showErrorView(forReason: 0)
return
}
print("Registration was successful!")
//Here we should go back to the home screen where the message at the top should say welcome back USER!
if let userID = user?.user.uid {
//Create User Profile
let databaseRef = Database.database().reference()
let usersRef = databaseRef.child("Users").child(userID)
let usernameValue = ["username":self.username.text]
usersRef.updateChildValues(usernameValue, withCompletionBlock: { (err, ref) in
if let err = err {
print(err.localizedDescription)
self.showErrorView(forReason: 2)
return
}
//Profile created and updated!
self.navigationController?.popViewController(animated: true)
})
}
})
}
}

Related

TableView not reloading with firebase data after dismissing modal controller

After logging in with firebase Auth, I try to update the home page tableview using a delegate except I get this issue -
2020-07-16 10:58:51.078331-0700 Appname[44300:8867431] [AXRuntimeCommon] Unknown client: Appname
2020-07-16 10:58:51.084416-0700 Appname[44300:8867435] [AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver' PID:44186
Once the app loads it checks if the user is logged in on the home page with this function
func isLoggedIn() {
if Firebase.Auth.auth().currentUser == nil {
perform(#selector(handleLogout), with: nil, afterDelay: 0)
}
}
#objc func handleLogout() {
do {
try Auth.auth().signOut()
} catch let logoutError {
print("logout error", logoutError)
}
let startview = StartView()
startview.home = self
let nav = UINavigationController(rootViewController: startview)
nav.modalPresentationStyle = .fullScreen
present(nav, animated: false)
}
Then in the login page it logs the user in and runs the function from the home page but it just shows up as blank.
#objc func Login() {
Auth.auth().signIn(withEmail: EmailField.text!, password: PasswordField.text!) { [weak self] (user, error) in
guard let StrongSelf = self else {
return
}
guard let result = user, error == nil else {
print(error!._code)
self?.handleError(error!)
return
}
let user = result.user
print("logged in \(user)")
//NotificationCenter.default.post(name: NSNotification.Name(rawValue: "loadhome"), object: nil)
StrongSelf.navigationController?.dismiss(animated: true, completion: {
self?.home.loadfirstusers()
})
}
}
var home = HomePage()
It calls this function to update the user data and gets as far as printing sameunisamecourse but it doesn't call the print inside the dispatch.notify for some reason?
func SameUniSameCourse(completion: #escaping (_ success: Bool) -> Void) {
self.dispatchGroup.enter()
service.loadUniversityAndCourse { (uni, course) in
defer{ self.dispatchGroup.leave() }
let usersRef = Firestore.firestore().collection("users").order(by: "Created", descending: true).whereField("University", isEqualTo: uni).whereField("Course", isEqualTo: course)
self.dispatchGroup.enter()
usersRef.getDocuments { (snapshot, error) in
print("samecoursesameuni")
defer{ self.dispatchGroup.leave() }
if let error = error {
print(error.localizedDescription)
} else {
for document in snapshot!.documents {
let data = document.data()
//print(data)
if let dictionary = data as [String:AnyObject]? {
let Info = UserInfo(dictionary: dictionary)
if Info.uid == Auth.auth().currentUser?.uid {
//print(Info.username)
}
else {
self.sameUniSameCourse.append(Info)
//print(Info.username!)
}}}
}
}}
self.dispatchGroup.notify(queue: .main) {
print("dispatchNotifyCalled")
if self.sameUniSameCourse.isEmpty == true {
completion(false)
}
else {
self.masterArray.append(contentsOf: self.sameUniSameCourse)
self.spinner.stopAnimating()
completion(true)
}
}
}

How to update user information in firebase?

i have this function to change user email. I'm able to change the email and i can see it in firebase console, but when i go return to my application to see user info, i see only the old email presented.
#IBAction func saveButton(_ sender: Any) {
let currentUser = Auth.auth().currentUser
currentUser?.updateEmail(to: emailTextField.text!) { error in
if let error = error {
print(error)
} else {
self.userDocRef = self.db.collection("users").document(self.auth.currentUser!.uid)
self.userDocRef?.getDocument(completion: {(snapshot, error) in
guard snapshot != nil else { print("Error:", error!); return }
let userData = snapshot!.data()!
self.emailTextField.text = userData["email"]! as? String
})
print("CHANGED")
}
}
}

Auth.auth().currentUser?.uid = nil iOS Firebase

Setting a variable to the value of Auth.auth().currentUser?.uid always returns nil even after the user has signed in, am I missing the fact that Firebase sometimes uses Async methods? The function used to return data from a Firebase realtime database is ran after the sign in function and is currently using a constant UID for testing purposes.
typealias RetrieveUserCompletionBlock = ((_ userType: String) -> Void)
func retrieveUserType(withBlock completion: #escaping RetrieveUserCompletionBlock){
let userTypeDB = Database.database().reference()
let currentUser = "kLxqZteRfBeC0bNIkLCjrPukMGx1"
var testUser = Auth.auth().currentUser?.uid
print(testUser)
userTypeDB.child("UserType").child(currentUser).observeSingleEvent(of: .value, with: {
(snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let email = value?["Email"] as? String ?? ""
completion(value?["User Type"] as? String ?? "")
}){
(error) in
completion("default value")
print(error.localizedDescription)
}
}
#IBAction func loginButtonPressed(_ sender: Any) {
SVProgressHUD.show()
Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
if error != nil {
//error
if let errorCode = AuthErrorCode(rawValue: error!._code) {
switch errorCode {
case .missingEmail:
SVProgressHUD.showError(withStatus: "Please enter a email in the text field")
SVProgressHUD.dismiss(withDelay: 2)
case .userDisabled:
SVProgressHUD.showError(withStatus: "Your account is disabled")
SVProgressHUD.dismiss(withDelay: 2)
case .invalidEmail:
SVProgressHUD.showError(withStatus: "Invalid email, please enter a valid email")
SVProgressHUD.dismiss(withDelay: 2)
case .wrongPassword:
SVProgressHUD.showError(withStatus: "Incorrect password")
SVProgressHUD.dismiss(withDelay: 2)
case .userNotFound:
SVProgressHUD.showError(withStatus: "Account details not found, please try again")
SVProgressHUD.dismiss(withDelay: 2)
default:
print("Error")
}
}
}
else {
//success
SVProgressHUD.showSuccess(withStatus: "Success")
SVProgressHUD.dismiss(withDelay: 1)
self.performSegue(withIdentifier: "goToMenuFromLogin", sender: self)
}
}
retrieveUserType { (userType) in
if userType != "Student"{
print("error")
} else {
print("success")
}
}
}

check if key exists firebase 4 and swift 4?

How do I check if a key exists in Firebase? I have seen this link here, but it's for Firebase 3, and it doesn't work for my situation. So for my case, I want to check to see if a username exists, and if it does, then don't register a user, but if it doesn't then register. I have something along the lines of this:
let usersDB = Database.database().reference().child("Users")
var taken = false
usersDB.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild(username) {
taken = true
self.errorLabel.text = "Username already taken."
}
})
if !taken {
// Email registration
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
print(error!.localizedDescription)
self.errorLabel.text = error!.localizedDescription
} else {
// Allows for username log in
usersDB.child(username).setValue(["email" : user?.email])
self.performSegue(withIdentifier: "goToGroups", sender: self)
}
})
}
The observeSingleEvent is what the previous similar post's solution was, but it only runs after I add a child on this line usersDB.child(username).setValue(["email" : user?.email]), it never runs before. Is there any other way to do this?
This looks like an issue with handling asynchronous calls. if !taken is very likely going to be checked before data is returned from observeSingleEvent because it's asynchronous, so the rest of the code will continue to run. One option is to move if !taken into the closure, like this:
let usersDB = Database.database().reference().child("Users/\(username)")
var taken = false
usersDB.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
taken = true
self.errorLabel.text = "Username already taken."
}
if !taken {
// Email registration
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
print(error!.localizedDescription)
self.errorLabel.text = error!.localizedDescription
} else {
// Allows for username log in
usersDB.child(username).setValue(["email" : user?.email])
self.performSegue(withIdentifier: "goToGroups", sender: self)
}
})
}
})

Firebase observe method won't return and continue

I'm writing some code for a login page where we take a username and find the associated password. Temporarily I've said "if email exists under username, complete segue". However when I call the method getEmail which checks for email, it never seems to exit properly with a full email address. print(email) returns the right email address so I know I've retrieved it and it's correct. I never seem to make it out of the method though. Really stuck here! Heres my code:
func getEmail(name: String) -> String{
var email = ""
ref = Database.database().reference()
self.ref.child("Users").child(name).observeSingleEvent(of: .value, with: { (snapshot) in
if let user = snapshot.value as? [String:Any] {
print("email retrieved");
email = user["email"] as! String;
print(email)
return;
}
else{
print("email could not be retrieved from the user.");
}
}){ (error) in
print("Could not retrieve object from database because: ");
print((Any).self);
}
return email;
}
func validate(){
if(Username.text == ""){
EmptyStringAlert();
}
let email = getEmail(name: Username.text!);
print(email)
if(email == ""){
return;
}
performSegue(withIdentifier: "LoginSuccess", sender: nil)
}
The call to Firebase is asynchronous, so you have to use completion in your function to get the data. Try something like this:
func getEmail(name: String, completion: #escaping (Bool, Any?, Error?) -> Void) {
var email = ""
ref = Database.database().reference()
self.ref.child("Users").child(name).observeSingleEvent(of: .value, with: { (snapshot) in
if let user = snapshot.value as? [String:Any] {
email = user["email"] as! String
completion(true, email, nil)
}
else {
completion(false, nil, nil)
}
}){ (error) in
completion(false, nil, error)
}
}
func validate(){
if(Username.text == ""){
EmptyStringAlert();
}
getEmail(name: Username.text!) { (success, response, error) in
guard success, let email = response as? String else {
print(error ?? "Failed getEmail..")
return
}
if(email == "") {
return
}
performSegue(withIdentifier: "LoginSuccess", sender: nil)
}
}

Resources