For some reason my firebase methods do things that make no sense to me. I want to login with a user and afterwards check on some data to make a decision. Both methods signIn()
and getDocument()
don't go beyond the curly brackets. If I set a breakpoint or step over the next point where it stops is outside the curly brackets. What am I doing wrong?
This is the whole code:
import Foundation
import Firebase
//@objc(LoginViewController)
class LoginViewController: UIViewController {
@IBOutlet weak var errorMessage: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBAction func didTapEmailLogin(_ sender: UIButton) {
// Check if empty
guard emailField.text != nil, passwordField.text != nil else {
self.errorMessage.text = "Fields can't be empty."
return
}
// Log in
let email = emailField.text!
let password = passwordField.text!
guard login(with: email, with: password) else {
print("Login didn't work")
return
}
// Check if user has a group yet
guard userHasGroup() else {
print("Getting data didn't work")
return
}
}
func userHasGroup() -> Bool {
var succesful = true
let db = Firestore.firestore()
let userUid = Auth.auth().currentUser?.uid
let docRef = db.collection("users").document(userUid!)
docRef.getDocument { (document, _) in
if let document = document, document.exists {
// Test
print(document.data() as! [String: Any])
} else {
succesful = false
}
}
return succesful
}
func login(with email: String, with password: String) -> Bool {
var succesful = true
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
guard error == nil, user != nil else {
// There was an error.
self.errorMessage.text = "Email/password incorrect."
succesful = false
return
}
}
return succesful
}
}