Reputation: 185
I have implemented authentication in Firebase via Google and have confirmed that users show up in my Authentication tab in Firebase console. I am now trying to search for users by their displayName
.
I am noticing that since these users show up under my authentication tab and not in my Database I can't search for them. When I search for users I get no results.
let rootRef = Database.database().reference()
let query = rootRef.child("users")
query.observe(.value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
if let value = child.value as? NSDictionary {
print(value)
}
}
}
Can someone let me know how I can search for users I have authenticated via Google signin? Do I need to also maintain a list of users in my database?
Upvotes: 0
Views: 601
Reputation: 26
In order to obtain information about who has signed up, you must add data to the firebase database. When you added let query = rootRef.child("users")
that is looking at the root of your firebase database for a collection of type 'users'. This must exist in order to retrieve a list of users.
What I would do is when you get a successful authentication back from firebase for the login, you obtain the current users uid currentUser.uid
and use that as the 'key' to your data.. such as rootRef.child(user.uid).setValue(["displayName": displayName])
. This adds a child node in your database with the uid of the user just created and adds something like 'displayName: Brandon' as the data associated with that user.
This way when you look up users, you'd get back the entire list of users under your 'users' node and then be able to access the data within each of the. It's also good practice to have the key to each node as the users uid. This is guaranteed to be unique and allows you to access the user's data quickly and easily.
Then you'd be able to use your code to read the users 'dictionary' values of what was stored.
For documentation on it read: https://firebase.google.com/docs/database/ios/read-and-write#read_data_once
Upvotes: 0
Reputation: 3064
What you need to do is implement Firebase Database as well as Firebase Auth. These are two different things. You can have a million people authenticate with your application and still have nothing in your database.
There are callbacks that return info about the user upon them creating their account. Within these callbacks, you should make calls to Firebase Database to take this user data, and store it on Firebase Database.
I've created a starter project that does all of this for you, however. It even lets your users authenticate with Facebook. Check it out: https://github.com/ChopinDavid/FirebaseLoginSignup
If you are wanting to do this on your own, however, here is an example of how to store user data after the Firebase Auth callback:
func createUserAcct(completion: @escaping (Bool, Error?) -> Void) {
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
if error == nil {
if let firebaseUser = Auth.auth().currentUser {
//After creating the user, I then take the user's inputs (email, display name, etc.) and create a profile change request
let changeRequest = firebaseUser.createProfileChangeRequest()
//Storing photoURLs requires that we make use of firebase Storage. We can upload an image, get a callback with the URL where the image was stored, and update our user's photoURL
changeRequest.photoURL = URL(string: "nil")
changeRequest.displayName = self.nameTextField.text!
changeRequest.commitChanges { error in
if let error = error {
// An error happened.
completion(false, error)
} else {
//This is where we actually create an "object" from the inputs the user has given us.
//We then take this object and store it in our database under "Users/{ the user's auth uid }"
let userData = ["email" : self.emailTextField.text!,"name": self.nameTextField.text!] as [String : Any]
Database.database().reference().child("Users").child(firebaseUser.uid).updateChildValues(userData)
}
}
}
} else {
// An error happened.
completion(false, error)
}
}
}
Upvotes: 1