When my page loads I need to know whether the user is logged in and perform different tasks based on that. The first thing I do is check if the they are logged in, but for some reason the if statement to check that is being validated twice. Here is my code:
override func viewDidAppear(_ animated: Bool) {
FIRAuth.auth()?.addStateDidChangeListener { auth, user in
if let user = user {
print("User is logged in")
}
"User is logged in" gets printed out twice to the console when the page loads instead of just once. Am I doing something incorrectly?
i faced same problem. and it is not for logged in . it can be faced flowing option https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener
so just crate a flag then check that one . or check response nil or not
Try this way :
override func viewDidAppear(_ animated: Bool) {
FIRAuth.auth()?.addStateDidChangeListener { auth, user in
if let user = user {
if user != nil {
print("User is logged in")
}
}
Note : try to use both flag and check user =! nil
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