I’m new to Swift and Firebase and am developing a ToDo List following this video: https://www.youtube.com/watch?v=K-4blUReYoU&list=PL5PR3UyfTWvei-pKlZN7d8r-0tHCK1EKE
I successfully created the app, then after 3 months, I opened the app to add more functions. However, the app was not able to display profile data including name, email, and joined date on the screen, even though Items on todolist page worked as usual.
I found that the Firebase warned “Your Cloud Firestore database is denying client requests and will continue to do so until you update your security rules”. So, I changed the security rule as below:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
However, it didn’t change my profile page.
I was testing to find out the problem in ProfileViewViewModel as below:
import FirebaseAuth
import FirebaseFirestore
import Foundation
class ProfileViewViewModel: ObservableObject{
init(){}
@Published var user: User? = nil
func fetchUser() {
print(" Call fetchuser")
guard let userId = Auth.auth().currentUser?.uid else {
print("No user ID found")
return
}
print("User ID: (userId)")
let db = Firestore.firestore()
db.collection("users")
.document(userId)
.getDocument{ [weak self] snapshot, error in
if let error = error {
print("Error fetching document: (error.localizedDescription)")
return
}
guard let data = snapshot?.data() else {
print("No document found")
return
}
print("Data fetched: (data)")
This is the outcome:
Call fetchuser
User ID: **************
No document found
It seems I received userID but couldn’t receive values in snapshot from Firebase.
Here is my data in Firebase
-
Authentication
-
Database
I love to know the what the problem is to understand firebase deeply.
Thank you.