Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Firebase authState upon viewDidDisappear

I'm using Firebase's new framework and I'm attempting to monitor the login state of the user on both the Login and Signup VC's separately. The problem is if the login state changes on the SignUp view the Auth State on the Login view gets called as well. My question is, how do I remove the auth state? I found the syntax on the Firebase website but am a little confused on what to pass in considering my code for the auth state:

FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
            if let theUser = user {
                // User is signed in.
                print("LOGGED IN!!!!  :::: \(theUser)")
                self.dismissViewControllerAnimated(true, completion: nil)
            } else {
                // No user is signed in.
                print("Need to login first.")
            }
        }

Code to use to remove the auth, but unsure what to pass in.

FIRAuth.auth()?.removeAuthStateDidChangeListener(FIRAuthStateDidChangeListenerHandle) 

Says I pass in a FIRAuthStateDidChangeListenerHandle, but how do I obtain this, or do I rewrite my authState code differently?

like image 580
Jamie22 Avatar asked Jun 05 '16 12:06

Jamie22


1 Answers

Just store the auth in a variable

self.authListener = FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
            if let theUser = user {
                // User is signed in.
                print("LOGGED IN!!!!  :::: \(theUser)")
                self.dismissViewControllerAnimated(true, completion: nil)
            } else {
                // No user is signed in.
                print("Need to login first.")
            }
        }

and remove it later

FIRAuth.auth()?.removeAuthStateDidChangeListener(self.authListener) 
like image 52
Shubhank Avatar answered Nov 15 '22 03:11

Shubhank