I'm trying to get the firstname from this:
The user is logged in and I can access his uid, but I can't get the Document that belongs to that user, this is how I'm trying to do it:
private func getDocument() {
var userID = Auth.auth().currentUser?.uid
userID = String(userID!)
var currentUser = Auth.auth().currentUser
// Get sspecific document from current user
let docRef = db.collection("users").document("bnf2s3RURUSV2Oecng9t")
// Get data
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: \(dataDescription)")
} else {
print("Document does not exist")
}
}
}
If you noticed, if I put manually "bnf2s3RURUSV2Oecng9t" it will access the data, but the point is to be able to know what ""bnf2s3RURUSV2Oecng9t" is so I can put it on that hard coded variable on top. My end goal is be able to get the first name of the current user :)
You can also create new password-authenticated users from the Authentication section of the Firebase console, on the Users page. Get the currently signed-in user The recommended way to get the current user is by setting a listener on the Auth object: Swift handle = Auth.auth().addStateDidChangeListener { auth, user in // ... Objective-C
To get a user's profile information, use the properties of an instance of FIRUser. For example: Swift let user = Auth.auth().currentUser if let user = user { // The user's ID, unique to the Firebase project. // Do NOT use this value to authenticate with your backend server, // if you have one. Use getTokenWithCompletion:completion: instead.
Get Started with the Firebase SDK Manage Users Password Authentication Email Link Authentication Google Sign-In Facebook Login Sign in with Apple Twitter GitHub Microsoft Yahoo Play Games Sign-in Phone Number Use a Custom Auth System Anonymous Authentication Link Multiple Auth Providers Passing State in Email Actions Web Sign in with a pre-built UI
Use Cloud Functions for Firebase Use Cloud Run Manage cache behavior Manage live & preview channels, releases, and versions Monitor web request data with Cloud Logging Usage, quotas, and pricing Deploy using the REST API
The problem is that your current authenticated user uid is not the same as the document uid. So normally if those ids where the same you would do something like this:
private func getDocument() {
//Get specific document from current user
let docRef = Firestore.firestore()
.collection("users")
.document(Auth.auth().currentUser?.uid ?? "")
// Get data
docRef.getDocument { (document, error) in
guard let document = document, document.exists else {
print("Document does not exist")
return
}
let dataDescription = document.data()
print(dataDescription?["firstname"] ?? "")
}
}
But in your case you would need to do it like this, since those values are not the same.
private func getDocument() {
//Get specific document from current user
let docRef = Firestore.firestore()
.collection("users")
.whereField("uid", isEqualTo: Auth.auth().currentUser?.uid ?? "")
// Get data
docRef.getDocuments { (querySnapshot, err) in
if let err = err {
print(err.localizedDescription)
} else if querySnapshot!.documents.count != 1 {
print("More than one document or none")
} else {
let document = querySnapshot!.documents.first
let dataDescription = document?.data()
guard let firstname = dataDescription?["firstname"] else { return }
print(firstname)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With