steven lee
steven lee

Reputation: 314

Firebase gives exception when logging out

Does any body know what the cause of this when I call try!

FIRAuth.auth()?.signOut()

this happens error here

ANSWER when we call signout, the user uid becomes nil while the observer still needs to uid to remove the observer. Therefore to fix it I use addAuthStateDidChangeListener

FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth: FIRAuth,user: FIRUser?) in
                if user != nil {
                    let controller = self.storyboard?.instantiateViewControllerWithIdentifier("ProfileViewController") as! ProfileViewController
                    controller.userID = (user?.uid)!
                    self.presentViewController(controller, animated: false, completion: nil)
                }
                else {
                    let controller = self.storyboard?.instantiateViewControllerWithIdentifier("LoginRegisterViewController") as! LoginRegisterViewController
                    self.presentViewController(controller, animated: false, completion: nil)
                }
            })

Upvotes: 2

Views: 775

Answers (1)

steven lee
steven lee

Reputation: 314

Problem when I go to this view, I will call the observer function, which I pass the (user?.uid)!. However when we log out, the (user?.uid)! will be nil while the observer still needs to call (user?.uid)!

Solution Therefore to fix this, I created a variable called userID in that class and pass the (user?.uid)! to userID when I move to that view

here is the code to move to that view

FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth: FIRAuth,user: FIRUser?) in
            if user != nil {
                let controller = self.storyboard?.instantiateViewControllerWithIdentifier("ProfileViewController") as! ProfileViewController
                controller.userID = (user?.uid)!
                self.presentViewController(controller, animated: false, completion: nil)
            }
            else {
                let controller = self.storyboard?.instantiateViewControllerWithIdentifier("LoginRegisterViewController") as! LoginRegisterViewController
                self.presentViewController(controller, animated: false, completion: nil)
            }
        })

Upvotes: 1

Related Questions