0

I'm coding in Swift with Firebase. I'm trying to understand something about authentication but it seems like that i can't get the point.

I need a function that check if a user is already logged in our platform. I need another function that check if the username and the password are both correct (if the user is in db)

Is there someone that could help me?

     func getLogin(username: String, pass: String) -> Int {
         print("getLogin in")
         var counter: Int = 0
         db.collection("users").whereField("username", isEqualTo: username).whereField("password", isEqualTo: pass).getDocuments() { (querySnapshot, err) in
             if let err = err {
                 print("Error getting documents: (err)")
                 counter = -1
                 print("\(counter)")
             } else {
                 for document in querySnapshot!.documents {
                     print("(document.documentID) => (document.data())")
                     counter += 1
                 }
                 print("\(counter)")
             }
         }
         return counter
     }
func getUser(username: String) -> Int {
         print("getUser in")
         var counter: Int = 0
         db.collection("users").document(username).whereField("username", isEqualTo: 
         username).getDocuments() { (querySnapshot, err) in
             for document in querySnapshot!.documents {
                 counter += 1
                 print("(document.documentID) => \(document.data())")
             }
         }
         return counter
     }
  • Both your code and "check if the username and the password are both correct (if the user is in db)" seem to indicate that you're **not** using Firebase Authentication, but you tagged with that. Can you confirm? – Frank van Puffelen Oct 14 '22 at 16:20
  • Yes i tried to use Firebase authentication but i didn't understand how to use it so i decided to continue with simple swift code – Andrea Scorza Oct 14 '22 at 16:28
  • I also recommend reading: https://stackoverflow.com/questions/1197417/why-are-plain-text-passwords-bad-and-how-do-i-convince-my-boss-that-his-treasur as storing password in plaintext in a database is always a bad idea and doing so in an intentionally publicly accessible database is even more dangerous. – Frank van Puffelen Oct 14 '22 at 16:28
  • "I didnb't understand how to use it" is hard to help with. I recommend starting with the Firebase documentation here: https://firebase.google.com/docs/auth/ios/password-auth. If you can't get that to work, edit your question to show us what you tried with calling *that* API. – Frank van Puffelen Oct 14 '22 at 16:29
  • Thank you so much, i'll try what you suggested. – Andrea Scorza Oct 14 '22 at 16:35

0 Answers0